Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Manipulating unknown types from c++.
Qt 6.12 LTS is out! See what's new in the release blog

Manipulating unknown types from c++.

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 3.5k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt
      • Unsolved