Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. dynamic_cast<type> equivalent in Qml?

dynamic_cast<type> equivalent in Qml?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 717 Views
  • 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.
  • C Offline
    C Offline
    Cyrille de Brebisson
    wrote on last edited by
    #1

    Hello,

    I have a C++ class (object factory), which notifies Qml when various object types are created with an onNewObject(object) signal.

    Qml creates the appropriate control (depends on a "type" property) and "assign" the object to it.
    Something like the pseudo code below.
    The problem that I am having is that Qml refuses to assign my "object" to the property as object is a QObject and it does not cast it to the proper type (or try to).
    So, my question is: how do I do the equivalent of a C++ dynamic_cast in Qml?
    Thanks.

    Component {
    id: componentType0
    property MyType0 source
    Rectangle { width: 10; height: 10; color: "Red" }
    }

    Component {
    id: componentType1
    property MyType1 source
    Rectangle { width: 10; height: 10; color: "Blue" }
    }

    onNewObject: { console.log("new object type:", object.type)
    switch (object.type) {
    case 0:
    componentType0.createObject(appWindow, {"source": object});
    break;
    case HPACDevices.Brain:
    componentType1.createObject(appWindow, {"source": object});
    break;
    default: return;
    }
    }

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      You cannot have property like property MyType0 source inside the component directly. I'm sure this is just a pseudo code.

      Instead of trying the MyType you can try with property var. Just see the following example.

      Component {
          id: componentType0
          Rectangle {
              property var source;
              width: 100; height: 100; color: "Red"
              onSourceChanged: {
                  source.visible=true;
              }
          }
      }
      

      Everything should work.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      1

      • Login

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