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. [SOLVED] Getting Object name/type using moc
Forum Update on Monday, May 27th 2025

[SOLVED] Getting Object name/type using moc

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.1k 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.
  • P Offline
    P Offline
    Paawan
    wrote on 28 Sept 2014, 17:22 last edited by
    #1

    Hi All,

    Need some help in using meta functionality in Qt. I am probably going wrong in many places.

    I have a Film Class. I have also derived (not sure if this is correct terminology) two more classes under Film, Educational and Entertainment which are sub-classes.

    I have a FilmMap class which is sub-class of QMap<QSting, Film*>.

    I have now written a third class FilmMapWriter which has write XML file of the FilmMap.

    @
    foreach (QString i, map1.keys()) {

       QVariant name = map1[i]->QObject->objectName();
        
       qDebug() << name.toString();
        qDebug()<< " In the FilmMapWriter class";
    
    }
    

    @

    In the above code I am trying to get the type of the object. If it is Film, Entertainment or Educational.

    But I am getting errors. Where am I going wrong?

    Thanks

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Paawan
      wrote on 28 Sept 2014, 17:37 last edited by
      #2

      I have changed the code to the following:

      @
      foreach (QString i, map1.keys()) {

         QString name = map1[i]->metaObject()->className();
      
      
         qDebug() << name;
      
      
          qDebug()<< " In the FilmMapWriter class";
      
      }
      

      @

      Now it is working. But the problem now is that it is showing all classes as Film.

      Not any of the sub-classes. Entertainment or Educational.

      How do I get capture that information.

      Thanks

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jeremy_k
        wrote on 28 Sept 2014, 20:41 last edited by
        #3

        Do the subclasses also have Q_OBJECT macros?

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Paawan
          wrote on 28 Sept 2014, 21:23 last edited by
          #4

          Hi,

          I did not. Added it and now it work.

          Thanks a lot for your time and assistance.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Paawan
            wrote on 28 Sept 2014, 22:20 last edited by
            #5

            Hi,

            I now have another issue.

            In the Film class I have declared 5 variables.

            In the sub-classes I have declared 2 more variables in each sub-class.

            For example in Film class I have declared a QString m_Director.

            I can access that by the following statement:

            @
            map1[i]->m_Director

            @

            Now I have declared QString m_Subject in the sub-class Educational.

            But when I use "->" I am not getting m_Subject option.

            How do I get it?

            Thanks again.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jeremy_k
              wrote on 29 Sept 2014, 00:12 last edited by
              #6

              It sounds like a general C++ inheritance tutorial such as http://www.cplusplus.com/doc/tutorial/inheritance/ will be a lot more useful than answers to individual questions.

              For this particular question, the problem might be an access specifier issue, or using a pointer to the base type instead of the derived one. Without seeing the source, it's hard to tell.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Paawan
                wrote on 29 Sept 2014, 20:30 last edited by
                #7

                Managed to do it. Used the qobject_cast operator and I am getting the results.

                Thanks for your time. Appreciated.

                1 Reply Last reply
                0

                5/7

                28 Sept 2014, 22:20

                • Login

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