P
P
Pavel K2016-01-24 22:09:56
Qt
Pavel K, 2016-01-24 22:09:56

How to pass QHash to qml?

Greetings!
I tried to do as they write in mana:

typedef   QHash<int, QObject*> TControllers;
Q_DECLARE_METATYPE(TControllers)
TControllers controllers;
controllers.insert(0, myObject);
QObject * r = engine.rootObjects().at(0);
r->setProperty("controls", QVariant::fromValue(controllers));

in qml
id: main
property var controls;
    MouseArea {
        anchors.fill: parent
        onClicked:  {
            console.log(main.controls);
            for (var p in main.controls) {
              console.log("control", p, main.controls[p]);
            }
        }
    }

Outputs only:
qml: QVariant(TControllers)
How to get it to work without converting to other types?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2016-01-27
@PavelK

QHash is not inherited from QObject, so just using it in QML will not work, because C++/QML integration works through Qt's meta-object system.
It is possible to write a hash wrapper class inherited from QObject that implements its properties and methods via Q_PROPERTY, Q_INVOKABLE, and public slots.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question