[Qt-qml] QDeclarativeProperty and custom qml types.
Simon Aridis-Lang
llamatronique at gmail.com
Sat Oct 23 17:51:37 CEST 2010
Hi All,
I'm having trouble with assigning qml item properties in C++. The type is declared in qml as a component, and is declared in another qml component as an item property.
i.e.
TypeA.qml
QtObject {
property string myString
}
TypeB.qml
QtObject{
property TypeA myTypeA
}
I'm creating each of the objects on the c++ side using something like:
QString qml("import Qt 4.7\nTypeB{}\n");
QDeclarativeComponent component(engine);
component.setData(qml.toLatin1(), QUrl(""));
QObject *myTypeB = component.create();
And then wrapping the property using:
QDeclarativeProperty typeAProperty(myTypeB, "myTypeA", engine);
.. where typeAProperty.isValid() returns true, and if I do a QDeclarativeProperty::read() I get a QVariant with a type of QVariant::QObject* which casts to QObject* correctly.
However when I write the value, using :
...
QObject *myTypeA = component.create()
typeAProperty.write(QVariant::fromValue(myTypeA));
.. it returns false which I guess means the type is incorrect according to the metatype, even though the QVariant's type is also QVariant::QObject*.
I know the object creation is working fine, as I can assign the object in the root context to a context property, set string and int properties and they are binding properly on the qml side. I couldn't find any examples in the documentation that specifically dealt with item properties. I've managed to get lists to work in a similar manner using QDeclarativeListReference, but the convention for item properties seems subtly different.
Regards,
Simon.
More information about the Qt-qml
mailing list