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. class with q_object macro declared and defined in .cpp
Forum Updated to NodeBB v4.3 + New Features

class with q_object macro declared and defined in .cpp

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.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.
  • U Offline
    U Offline
    user4592357
    wrote on 3 Oct 2017, 20:14 last edited by
    #1

    i know that one can't have a nested class which has Q_OBJECT macro defined in it, in a class (even though i don't quite well understand the reasons, if someone explained i'd be very happy)

    but my question here is about something else. as i can't have a nested (private) class, i'd like to place that clasa in .cpp file, as the title suggests, which i did and got "undefined reference for vtable" error, while my class had a virtual dtor (it's derived from QWidget). but when i place the class in .h file, it works fine. what's the reason for this?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 3 Oct 2017, 20:21 last edited by
      #2

      Hi,

      You are missing the moc file inclusion at the bottom of the your code.

      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
      3
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 3 Oct 2017, 20:25 last edited by
        #3

        The reason is:
        The moc tool that uses the Q_OBJECT and generate mappings for signals and slot are run on .h files only.

        1 Reply Last reply
        3
        • U Offline
          U Offline
          user4592357
          wrote on 4 Oct 2017, 03:13 last edited by user4592357 10 Apr 2017, 03:15
          #4

          so how can i "hide" this class? it's something like this:

          class A : public QWidget {/* definition */};
          
          class B : public QWidget {
               A *a;
               // other members
               // other functions
          };
          

          of course i wanna "hide" class A

          M 1 Reply Last reply 4 Oct 2017, 06:21
          0
          • U user4592357
            4 Oct 2017, 03:13

            so how can i "hide" this class? it's something like this:

            class A : public QWidget {/* definition */};
            
            class B : public QWidget {
                 A *a;
                 // other members
                 // other functions
            };
            

            of course i wanna "hide" class A

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 4 Oct 2017, 06:21 last edited by mrjj 10 Apr 2017, 06:21
            #5

            @user4592357
            Hi
            You can use the .cpp file as @SGaist suggests.

            more info
            https://stackoverflow.com/questions/34928933/why-is-important-to-include-moc-file-at-end-of-a-qt-source-code-file

            1 Reply Last reply
            0
            • P Pl45m4 referenced this topic on 27 Nov 2023, 05:29

            1/5

            3 Oct 2017, 20:14

            • Login

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