Manipulating unknown types from c++.



  • Hi,

    I'm trying to do some manipulation (serialisation/deserialisation etc) of qml types in c++, and am having some difficulty with properties of unknown types (i.e. for which there are only qml components).

    Let's say I have the following qml objects.

    TypeA.qml
    @import Qt 4.7
    QtObject {
    property string name
    property int number
    }
    @
    TypeB.qml
    @import Qt 4.7
    QtObject {
    property TypeA singleTypeA
    property list<TypeA> lotsOfAs
    }
    @

    In native code I want to be able to do normal stuff like create, add, remove & modify the properties in TypeB. I can at the moment set and get properties fine on objects like TypeA, because it's properties are QVariants of known types.

    Do qml types inherently shallow copy? or is there some way using Object::setProperty and Object::property to access instances of unknown qml types directly (i.e. to their base QObject* or anything else that can be cast using qobject_cast)? The objects are in fairly complex hierarchies and I'd prefer to be working on pointers directly to those objects.

    In the case of the list, if I look at the metaproperty, it is of type QVariant::UserType, and the typename is e.g. QDeclarativeListProperty<TypeA_QMLTYPE_2>. My understanding is that QDeclarativeListProperty is a wraper for an underlying list instance (say of type QObject*), but I don't know how to get from the QVariant to something like QList<QObject*>*.

    Cheers,
    Simon



  • Found the answers...

    For QObjects they can just be cast using qvariant_cast<QObject*>(variant).
    For qml created lists use a QDeclarativeListReference, making sure to pass in the engine.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.