Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Manipulating unknown types from c++.

    QML and Qt Quick
    1
    2
    2949
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • L
      llamatron last edited by

      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

      1 Reply Last reply Reply Quote 0
      • L
        llamatron last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • First post
          Last post