QML - Passing an associative Javascript array in C ++

In my application, I have a class that is registered as singleton for QML. My goal is to collect the values ​​in QMLas an associative array and pass that array to C++. This is a 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 am registering an instance of the class as singleton:

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

At some point in the QML file, I am trying to pass a javascript array back to C ++;

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

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

+4
source share
1 answer

, ) , , , Qt JS QVariantList JS QVariantMap , :

var arr = {};
+4

Source: https://habr.com/ru/post/1545770/


All Articles