Connecting a dynamic QMap to Qml
-
wrote on 6 Aug 2015, 08:38 last edited by Maxim DC 8 Jun 2015, 08:41
Let's say i have a
QMap<MyKey*, MyObject*> myobjects
, it it possible to connect this to qml throughQQmlListProperty<MyObject>
PS: MyKey is not needed in qml, wich can be thrown away with
myobjects.values()
but that returns a pointer to aQList<MyObjects>
but that throws an segmentation error when creating qqmllistproperty -
Let's say i have a
QMap<MyKey*, MyObject*> myobjects
, it it possible to connect this to qml throughQQmlListProperty<MyObject>
PS: MyKey is not needed in qml, wich can be thrown away with
myobjects.values()
but that returns a pointer to aQList<MyObjects>
but that throws an segmentation error when creating qqmllistproperty@Maxim-DC If you don't need
MyKey*
in QML then you can useQVariantMap
with custom objects as values. The custom object will need to be registered first usingqmlRegisterType
. Also sinceQVariantMap
usesQVariant
as 2nd parameter you will need to make that custom objectQVariant
. To sum up,
For eg.//register MyClass before using qmlRegisterType<MyClass>("MyClass",1,0,"MyClass"); MyClass *myclass = new MyClass; myclass->setText("MyClass Item"); //some method in MyClass //add it to QVariant QVariant var = QVariant::fromValue(myclass); //add it to QVariantMap QVariantMap map; map.insert("myclass",var); //set the map as context property engine.rootContext()->setContextProperty("mapObj",QVariant::fromValue(map)); //access from QML import MyClass 1.0 console.log(mapObj["myclass"]) console.log(mapObj["myclass"].text()) //text() is a public slot in MyClass
Also make sure
MyClass
inheritsQObject
-
@Maxim-DC If you don't need
MyKey*
in QML then you can useQVariantMap
with custom objects as values. The custom object will need to be registered first usingqmlRegisterType
. Also sinceQVariantMap
usesQVariant
as 2nd parameter you will need to make that custom objectQVariant
. To sum up,
For eg.//register MyClass before using qmlRegisterType<MyClass>("MyClass",1,0,"MyClass"); MyClass *myclass = new MyClass; myclass->setText("MyClass Item"); //some method in MyClass //add it to QVariant QVariant var = QVariant::fromValue(myclass); //add it to QVariantMap QVariantMap map; map.insert("myclass",var); //set the map as context property engine.rootContext()->setContextProperty("mapObj",QVariant::fromValue(map)); //access from QML import MyClass 1.0 console.log(mapObj["myclass"]) console.log(mapObj["myclass"].text()) //text() is a public slot in MyClass
Also make sure
MyClass
inheritsQObject
-
@p3c0 Ok i got it but then i have to specify the key in qml, thats not what i want, i want to make a listmodel of the values of the qmap
-
wrote on 6 Aug 2015, 11:14 last edited by Maxim DC 8 Jun 2015, 11:16
@p3c0 For example the qmap consists of 3 objects,
<mykey1*, myobject1*> <mykey2*, myobject2*> <mykey3*, myobject3*>
i want to show a list with properties, textinitem1: myobject1.somevar, textinitem2: myobject2.somevar, .... -
@p3c0 For example the qmap consists of 3 objects,
<mykey1*, myobject1*> <mykey2*, myobject2*> <mykey3*, myobject3*>
i want to show a list with properties, textinitem1: myobject1.somevar, textinitem2: myobject2.somevar, ....@Maxim-DC I doubt that you can assign a
QMap
as a model toListView
. -
@p3c0 For example the qmap consists of 3 objects,
<mykey1*, myobject1*> <mykey2*, myobject2*> <mykey3*, myobject3*>
i want to show a list with properties, textinitem1: myobject1.somevar, textinitem2: myobject2.somevar, ....@Maxim-DC If you want to reuse your existing
QMap
then it would be better to create a new class based on QAbstractIItemModel. In there you can re-implementdata()
to return your data fromQMap
.
8/8