How to inherit from QObject directly?



  • 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.



  • 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);

    .....

    };
    @



  • 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.



  • ^^^ 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.



  • 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.



  • 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.



  • 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 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++.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.