Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Finding Classes By Meta Information
Qt 6.11 is out! See what's new in the release blog

Finding Classes By Meta Information

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.8k 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.
  • webzoidW Offline
    webzoidW Offline
    webzoid
    wrote on last edited by
    #1

    Having registered several class types using the qRegisterMetaType function, is there any way I'm able to iterate through these registered classes and check for specific type inheritance?

    I want to build a list of specific classes which derive from a certain base class and then add these into a QListWidget.

    Not sure even where to begin.

    kshegunovK 1 Reply Last reply
    0
    • webzoidW webzoid

      Having registered several class types using the qRegisterMetaType function, is there any way I'm able to iterate through these registered classes and check for specific type inheritance?

      I want to build a list of specific classes which derive from a certain base class and then add these into a QListWidget.

      Not sure even where to begin.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @webzoid said in Finding Classes By Meta Information:

      is there any way I'm able to iterate through these registered classes

      I don't believe so, you should keep that information yourself.

      check for specific type inheritance?

      You can do that. Suppose you have a class X that's registered as a metatype, and you want to check if it's derived from a class Y (also registered as a metatype). Then you can pull the meta-object for that class and check it against the one for class Y. Something along those lines (Qt 5.7+):

      const QMetaObject * xMetaObject = QMetaType::metaObjectForType(qMetaTypeId<X>());
      const QMetaObject * yMetaObject = QMetaType::metaObjectForType(qMetaTypeId<Y>());
      Q_ASSERT(xMetaObject && yMetaObject);
      
      if (xMetaObject->inherits(yMetaObject))
          ; // X is derived from Y;
      else
          ; // ... it is not
      

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      2
      • webzoidW Offline
        webzoidW Offline
        webzoid
        wrote on last edited by
        #3

        @kshegunov Thank you for your comprehensive response.

        The last bit is very useful but its a shame that I can't iterate through registered meta types. I s'pose I could always have my own function which registers the type into a list (maintaining the MetaTypeId for instance as well as calling qRegisterMetaType.

        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 Extensions
        • Unsolved