QML - Passing Javascript associative array to C++

In my app I have a class which registered as singleton for QML. My purpose is collecting values in QML as associative array and passing this array to C++. This is the simplified version of the class:

class Config : public QObject
{
Q_OBJECT
private:
  Config(QObject *parent = 0);
public:
  static Config *instance();
  ~Config();
  Q_INVOKABLE void sendValue (const QVariantMap &map) {
    qWarning() << map.size();
  }
}

and here I register an instance of the class as singleton:

qmlRegisterSingletonType("myNS", 1, 0, "Config", config_singletontype_provider);

In some place in QML file I try to pass javascript array back to c++;

function sendValue() {
  var arr = [];
  arr["key"] = "value";
  Config.sendValue(arr);      
}

But nothing passed. The map.size() in C++ returns 0. May be I need some additional conversion?

 

 

Ok, I answer to my own question ) The documentation is not so clear but, as I understand, Qtconverts JS array to QVariantList and JS object to QVariantMap So in my case I just need to create an object, not array:

var arr = {};

遍历: 


        QVariantList lst;
        QVariant var;
        QVariantList::iterator iteLst = lst.begin();
        for (iteLst = lst.begin(); iteLst != lst.end(); iteLst++)
        {
                var = *iteLst;
        }

 

你可能感兴趣的:(QT)