Home > database >  QMap Datatype in QML basic usage?
QMap Datatype in QML basic usage?

Time:10-07

Assume I have a simple QMap declared and Initialized with values inside C and correctly exposed to QML.

C

QMap<QString, QMap<QString, QString>> airflow_buttons_;
//simple getter
[[nodiscard]] QMap<QString, QMap<QString, QString>> airflow_buttons() const;

QML

console.log(ClimateModel.airflow_buttons);

prints the following

(qrc:/Climate/AirflowButtons.qml:24) - QVariant(QMap<QString,QMap<QString,QString>>, QMap(("left", QMap(("top_air", "NOT_ACTIVE")))))

It is giving me a QVariant. I have no idea how to convert it to my desired Map with the desired values?

I want to use it as a simple JavaScript Map.

console.log(ClimateModel.airflow_buttons["left"]["top_air"]); //Should print "NOT_ACTIVE" 
console.log(ClimateModel.airflow_buttons["left"]);            //Should print the Map contents 

The current error message is [debug] expression for onCompleted (qrc:/Climate/AirflowButtons.qml:24) - undefined

Example on what i want to achieve in plain Javascript

var myMap = {x: "Test", y: "Test 2"};
myMap["z"] = "Test 3";
console.log(myMap["z"]);                 //prints "Test 3"
console.log(JSON.stringify(myMap["z"])); //prints the map contents

How can I achieve this trivial task?

CodePudding user response:

You should be using a QVariantMap all the way:

QVariantMap airflow_buttons = {
    {"left", QVariantMap{
         {"top_air", "NOT ACTIVE"}}}
};

btw, in QML you can also do this if you fancy:

console.log(ClimateModel.airflow_buttons.left.top_air)
  • Related