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] QMetaObject::className() returns base name
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QMetaObject::className() returns base name

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.7k 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.
  • G Offline
    G Offline
    goocreations
    wrote on last edited by
    #1

    I need to to retrieve an object's class to save some values to an XML file. I'm able to use typeid, but just tried to use Qt for it and ran into a problem.

    I have a class B inheriting from class A (A inherits from QObject). I have a manager class M which has a QList<A*>, but actually stores B objects. Inside B I have a function exportXml():

    @void B::exportXml()
    {
    cout << metaObject()->className() << endl;
    }
    @

    The thing is that the output statement prints "A" rather than what I expected: "B". I think it might be because I'm storing the B objects as A-pointers in M and that something might go wrong due to polymorphism. But since I'm calling metaObject() from inside B, shouldn't this print B instead of A, or am I doing something wrong?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      panosk
      wrote on last edited by
      #2

      Is exportXml() also implemented in A? In that case, maybe you have to make it virtual so it resolves to the derived class (B).

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goocreations
        wrote on last edited by
        #3

        exportXml() is a virtual function in A and is re-implemented in B

        1 Reply Last reply
        0
        • P Offline
          P Offline
          panosk
          wrote on last edited by
          #4

          Have you included the Q_Object macro in your B class as well?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goocreations
            wrote on last edited by
            #5

            Ahh, adding Q_OBJECT to the subclass did the trick. Thanks panosk.

            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