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. How to inherit from QObject directly?
QtWS25 Last Chance

How to inherit from QObject directly?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 6 Posters 37.3k 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.
  • L Offline
    L Offline
    lanmanck
    wrote on 9 May 2013, 02:15 last edited by
    #1

    Hi,Guys:

    I define a new class and found it cannot inherit from QObject, the complier will report an error:undefined reference to vtable for XX

    an example from here:http://harmattan-dev.nokia.com/docs/library/html/qt4/unix-signals.html
    cannot be compiled correctly.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 9 May 2013, 06:10 last edited by
      #2

      Hi,

      you can inherit directly from QObject.
      If you class implements signals or slots you must provide Q_OBJECT macro in provate part of you class definition.

      For Example

      @
      class MyObject: public QObject
      {
      Q_OBJECT

      public:
      MyObject (QObject *_parent);

      .....

      };
      @

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mranger90
        wrote on 9 May 2013, 12:20 last edited by
        #3

        Also note that the class definition has to be in a separate include file or the moc will not generate the code. A common cause of this error is to have the class definition and the code in main.cpp.

        1 Reply Last reply
        0
        • U Offline
          U Offline
          utcenter
          wrote on 9 May 2013, 12:27 last edited by
          #4

          ^^^ That is not necessary - you can do the implementation entirely in the header file, the reason you also need the cpp file is because the moc generated code will be inserted there. You can pretty much leave an empty cpp file, but without it you will get compilation error.

          1 Reply Last reply
          1
          • L Offline
            L Offline
            lanmanck
            wrote on 9 May 2013, 14:26 last edited by
            #5

            thanks guys.
            I read this thread:http://qt-project.org/forums/viewthread/1227/
            and in main.c I MUST construct the class, or it will report an compile error.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mranger90
              wrote on 9 May 2013, 16:02 last edited by
              #6

              utCenter - you are correct. But I was addressing this situation:

              @
              #include "mainwindow.h"
              #include <QApplication>

              class MyClass: public QObject
              {
              Q_OBJECT

              public:
              MyClass(QObject *parent) : QObject(parent)
              {}

              };

              int main(int argc, char *argv[])
              {
              QApplication a(argc, argv);
              MainWindow w;

              MyClass *m = new MyClass(0);
              
              w.show();
              
              return a.exec&#40;&#41;;
              

              }
              @

              This is a common cause of the undefined vtable error.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on 9 May 2013, 17:31 last edited by
                #7

                In the vast majority of cases you get the vtable error because you didn't re-run qmake after adding the Q_OBJECT macro to a file (which means that your makefiles are missing a call to moc).

                There is no need to create a seperate (empty) .cpp file and moc does not modify or insert code to any files. It generates an additional file called <filename>.moc which has to be added manually if there is no seperate .cpp file by including the file to any compilation unit which gets linked against the binary.

                @
                #include "mainwindow.h"
                #include <QApplication>

                class MyClass: public QObject
                {
                Q_OBJECT

                public:
                MyClass(QObject *parent) : QObject(parent)
                {}

                };

                #include "main.moc" // Include the generated code

                int main(int argc, char *argv[])
                {
                QApplication a(argc, argv);
                MainWindow w;

                MyClass *m = new MyClass(0);
                
                w.show();
                
                return a.exec(&#41;;
                

                }
                @

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lanmanck
                  wrote on 10 May 2013, 00:49 last edited by
                  #8

                  [quote author="Lukas Geyer" date="1368120690"]In the vast majority of cases you get the vtable error because you didn't re-run qmake after adding the Q_OBJECT macro to a file (which means that your makefiles are missing a call to moc).

                  There is no need to create a seperate (empty) .cpp file and moc does not modify or insert code to any files. It generates an additional file called <filename>.moc which has to be added manually if there is no seperate .cpp file by including the file to any compilation unit which gets linked against the binary.

                  @
                  #include "mainwindow.h"
                  #include <QApplication>

                  class MyClass: public QObject
                  {
                  Q_OBJECT

                  public:
                  MyClass(QObject *parent) : QObject(parent)
                  {}

                  };

                  #include "main.moc" // Include the generated code

                  int main(int argc, char *argv[])
                  {
                  QApplication a(argc, argv);
                  MainWindow w;

                  MyClass *m = new MyClass(0);
                  
                  w.show();
                  
                  return a.exec(&#41;;
                  

                  }
                  @
                  [/quote]

                  So this is a QT issue, not C++.

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    ChrisW67
                    wrote on 10 May 2013, 03:17 last edited by
                    #9

                    I categorise this as a not-knowing-how-the-tools-work problem.

                    The C++ linker is complaining because the C++ program is incomplete. The program is incomplete because the C++ source that moc generates for QObject classes is missing. That source is missing because the header/source containing the QObject subclass declaration is not listed in HEADERS/SOURCES, its Q_OBJECT macro is missing, or the Makefile does not contain the relevant moc commands because qmake has not been run since a moc-signifcant change was made to source files.

                    In a one-file program, e.g. main.cpp, with QObjects moc can be triggered with a:
                    @
                    #include "main.moc"
                    @
                    at the bottom.

                    Just to expand a bit: The Q_OBJECT macro expands to declarations including one for metaObject(). The "vtable":http://www.wikipedia.org/wiki/Virtual_method_table is a compiler generated table used to direct calls to virtual functions to the correct implementation at run time. The moc generated code implements the virtual function metaObject() for the class containing the Q_OBJECT macro, which is why the vtable gets mentioned when the implementation is missing.

                    1 Reply Last reply
                    0

                    4/9

                    9 May 2013, 12:27

                    • Login

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