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. get a QML Item's type name in C++
Forum Updated to NodeBB v4.3 + New Features

get a QML Item's type name in C++

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 3 Posters 1.4k 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.
  • P Offline
    P Offline
    poncho524
    wrote on last edited by
    #1

    Let's say you have two QML items defined...

    ThingA {
      property int a: 0
      property int b: 0
    ...
    }
    
    ThingB {
      property int c:0
      property int d:0
    ...
    }
    

    (using 4.8 here...)
    From C++ if i get a QDeclarativeItem*, can I determine if it is a ThingA or ThingB, so i can determine which property's i can change?

    ODБOïO Pablo J. RoginaP 2 Replies Last reply
    0
    • P poncho524

      Let's say you have two QML items defined...

      ThingA {
        property int a: 0
        property int b: 0
      ...
      }
      
      ThingB {
        property int c:0
        property int d:0
      ...
      }
      

      (using 4.8 here...)
      From C++ if i get a QDeclarativeItem*, can I determine if it is a ThingA or ThingB, so i can determine which property's i can change?

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      @poncho524 hi,
      QDeclarativeItem : http://doc.qt.io/archives/qt-4.8/qdeclarativeitem.html
      has 1 property inherited from QObject : objectName : QString http://doc.qt.io/archives/qt-4.8/qobject.html#properties

      P 1 Reply Last reply
      0
      • P Offline
        P Offline
        poncho524
        wrote on last edited by
        #3

        I found a hack solution. But its not really documented so i'm not sure its portable or stable.

        But this will return a string that CONTAINS the item type, but with other stuff too...

        item->metaObject()->className()
        

        if used in the case above, this would return "ThingA_QMLTYPE_3"
        where "3" could be any number apparently.
        Still interested if anyone knows of something better or documented.

        1 Reply Last reply
        0
        • ODБOïO ODБOï

          @poncho524 hi,
          QDeclarativeItem : http://doc.qt.io/archives/qt-4.8/qdeclarativeitem.html
          has 1 property inherited from QObject : objectName : QString http://doc.qt.io/archives/qt-4.8/qobject.html#properties

          P Offline
          P Offline
          poncho524
          wrote on last edited by
          #4

          @LeLev objectName doesn't give type... this is a user property that is usually NULL unless explicitly assigned.

          ODБOïO 1 Reply Last reply
          0
          • P poncho524

            @LeLev objectName doesn't give type... this is a user property that is usually NULL unless explicitly assigned.

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by
            #5

            @poncho524 yes i checked that yesterday sry..

            1 Reply Last reply
            0
            • P poncho524

              Let's say you have two QML items defined...

              ThingA {
                property int a: 0
                property int b: 0
              ...
              }
              
              ThingB {
                property int c:0
                property int d:0
              ...
              }
              

              (using 4.8 here...)
              From C++ if i get a QDeclarativeItem*, can I determine if it is a ThingA or ThingB, so i can determine which property's i can change?

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #6

              @poncho524 said in get a QML Item's type name in C++:

              From C++ if i get a QDeclarativeItem*, can I determine if it is a ThingA or ThingB, so i can determine which property's i can change?

              Could it be possible you describe the reason why the C++ side should update QML properties?

              It looks like you might be doing all the way around, I mean, if something changed in the C++ side and you want to show that change in the UI you may want to react to notifications from C++ (using the Q_PROPERTY macro, see this article)

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              2

              • Login

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