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. QMetaObject questions
Qt 6.11 is out! See what's new in the release blog

QMetaObject questions

Scheduled Pinned Locked Moved General and Desktop
qmetaobject
6 Posts 4 Posters 2.6k Views 3 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.
  • G Offline
    G Offline
    GrahamL
    wrote on last edited by
    #1

    Hi
    Say I have a method that takes a point to a base class and I provide this method with a pointer to a class derived from the base.
    Now, I want to use QMetaObject to get the class name

    void myMethod(Base* ptr)
    {
        QString s = ptr->metaObject()->className();
    }
    

    class name is always Base

    Is there a wayuof getting the derived class name?

    Thanks

    JKSHJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you show the declaration of your Base and Derived classes ?

      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
      • G GrahamL

        Hi
        Say I have a method that takes a point to a base class and I provide this method with a pointer to a class derived from the base.
        Now, I want to use QMetaObject to get the class name

        void myMethod(Base* ptr)
        {
            QString s = ptr->metaObject()->className();
        }
        

        class name is always Base

        Is there a wayuof getting the derived class name?

        Thanks

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        Hi,

        @GrahamL said:

        class name is always Base

        Add the Q_OBJECT macro to your derived class: http://doc.qt.io/qt-5/qobject.html#Q_OBJECT

        Then, run qmake again and recompile your program.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Devopia
          wrote on last edited by
          #4

          Hi,

          void myMethod(Base* ptr)
          {
          auto derivedClass = ptr->metaObject()->superClass();
          if (derivedClass)
          QString s = derivedClass->className();
          }

          JKSHJ 1 Reply Last reply
          0
          • D Devopia

            Hi,

            void myMethod(Base* ptr)
            {
            auto derivedClass = ptr->metaObject()->superClass();
            if (derivedClass)
            QString s = derivedClass->className();
            }

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            @Devopia said:

            void myMethod(Base* ptr)
            {
            auto derivedClass = ptr->metaObject()->superClass();
            if (derivedClass)
            QString s = derivedClass->className();
            }

            superClass() points to the base class, not the derived class...

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • G Offline
              G Offline
              GrahamL
              wrote on last edited by
              #6

              Hi guys

              Thanks for your replies
              It turns out the the derived class did not have the Q_OBJECT macro

              All working now

              Thanks

              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