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. How to check from C++ whether an object is of QML type Foo

How to check from C++ whether an object is of QML type Foo

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
c++typeqobjectcast
8 Posts 3 Posters 4.7k 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.
  • S Offline
    S Offline
    Stefan Monov76
    wrote on 18 Jan 2017, 13:31 last edited by Stefan Monov76
    #1

    Consider a QML type Foo (defined in QML, not in C++):

    // in Foo.qml
    Item {
        // ...
    }
    

    And assume I have a QQuickItem* item variable in C++. How do I check if the variable is of type Foo?

    If Foo was a C++ type, I could do this:

    qobject_cast<Foo*>(item) == nullptr
    

    Since Foo is a QML type, one option would be

    item->metaObject()->className().beginsWith("Foo")
    

    (className() returns something like Foo_QMLTYPE_0)

    But that seems unreliable and hacky.

    R 1 Reply Last reply 18 Jan 2017, 14:37
    0
    • S Stefan Monov76
      18 Jan 2017, 13:31

      Consider a QML type Foo (defined in QML, not in C++):

      // in Foo.qml
      Item {
          // ...
      }
      

      And assume I have a QQuickItem* item variable in C++. How do I check if the variable is of type Foo?

      If Foo was a C++ type, I could do this:

      qobject_cast<Foo*>(item) == nullptr
      

      Since Foo is a QML type, one option would be

      item->metaObject()->className().beginsWith("Foo")
      

      (className() returns something like Foo_QMLTYPE_0)

      But that seems unreliable and hacky.

      R Offline
      R Offline
      Roumed
      wrote on 18 Jan 2017, 14:37 last edited by
      #2

      @Stefan-Monov76

      Isn't it work the same?
      If you have and item for requesting metaObject(), you still can use qobject_cast()

      S 1 Reply Last reply 18 Jan 2017, 15:11
      0
      • R Roumed
        18 Jan 2017, 14:37

        @Stefan-Monov76

        Isn't it work the same?
        If you have and item for requesting metaObject(), you still can use qobject_cast()

        S Offline
        S Offline
        Stefan Monov76
        wrote on 18 Jan 2017, 15:11 last edited by
        #3

        @Roumed: I can't use qobject_cast, because Foo is not a C++ class (it's a QML type). I can't pass it as a template parameter to qobject_cast.

        R 1 Reply Last reply 18 Jan 2017, 15:24
        0
        • S Stefan Monov76
          18 Jan 2017, 15:11

          @Roumed: I can't use qobject_cast, because Foo is not a C++ class (it's a QML type). I can't pass it as a template parameter to qobject_cast.

          R Offline
          R Offline
          Roumed
          wrote on 18 Jan 2017, 15:24 last edited by
          #4

          @Stefan-Monov76
          Every QML type is a C++ type, registred for QML.
          For example WebViewLoadRequestin in QML is QQuickWebViewLoadRequest in C++.

          So since your item has metaObject() it has to be QObject derived.
          The only question is which type is it.

          S 1 Reply Last reply 19 Jan 2017, 13:15
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 18 Jan 2017, 22:03 last edited by
            #5

            Hi,

            Out of curiosity, why do you need that ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • R Roumed
              18 Jan 2017, 15:24

              @Stefan-Monov76
              Every QML type is a C++ type, registred for QML.
              For example WebViewLoadRequestin in QML is QQuickWebViewLoadRequest in C++.

              So since your item has metaObject() it has to be QObject derived.
              The only question is which type is it.

              S Offline
              S Offline
              Stefan Monov76
              wrote on 19 Jan 2017, 13:15 last edited by Stefan Monov76
              #6

              @Roumed: Not every QML type is a C++ type registered for QML. Types defined in QML are not C++ types. I think I see where the confusion comes from. I had written the example QML type declaration Foo { } in my original post, but what I actually meant was a file called Foo.qml containing an Item as the root object. Edited.

              @SGaist: I'm iterating over the children of a QML item Container and using them as texture sources for custom OpenGL rendering. But I want to fetch just those children that I have coded, not the ones that Qt adds silently. E.g. if I put the Container in a layout, then Qt inserts something like a QQuickLayoutAttached object as a child of the Container.

              R 1 Reply Last reply 19 Jan 2017, 13:39
              0
              • S Stefan Monov76
                19 Jan 2017, 13:15

                @Roumed: Not every QML type is a C++ type registered for QML. Types defined in QML are not C++ types. I think I see where the confusion comes from. I had written the example QML type declaration Foo { } in my original post, but what I actually meant was a file called Foo.qml containing an Item as the root object. Edited.

                @SGaist: I'm iterating over the children of a QML item Container and using them as texture sources for custom OpenGL rendering. But I want to fetch just those children that I have coded, not the ones that Qt adds silently. E.g. if I put the Container in a layout, then Qt inserts something like a QQuickLayoutAttached object as a child of the Container.

                R Offline
                R Offline
                Roumed
                wrote on 19 Jan 2017, 13:39 last edited by
                #7

                @Stefan-Monov76 , I see you, agree.
                If understand you right about your task about OpenGL rendering, you need only visual items.
                So, how about iterate not over children but over childItems?

                And about checking qml types: I think checking meta object name is the only way.

                S 1 Reply Last reply 19 Jan 2017, 14:07
                1
                • R Roumed
                  19 Jan 2017, 13:39

                  @Stefan-Monov76 , I see you, agree.
                  If understand you right about your task about OpenGL rendering, you need only visual items.
                  So, how about iterate not over children but over childItems?

                  And about checking qml types: I think checking meta object name is the only way.

                  S Offline
                  S Offline
                  Stefan Monov76
                  wrote on 19 Jan 2017, 14:07 last edited by
                  #8

                  @Roumed: Thanks, the childItems suggestion sounds good, I'll implement it as it's more semantically correct. But for now I'll combine it with the isFoo workaround described here, because I'm not sure if Qt won't decide to insert visual children in my item if I change something in the future. Just like I didn't expect it would insert nonvisual children.

                  1 Reply Last reply
                  0

                  1/8

                  18 Jan 2017, 13:31

                  • Login

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