tags:

views:

1465

answers:

5

I'm developing a simple qt4 app and making my own dialog. I subclassed QDialog, inserted Q_OBJECT macro in class declaration block and..

I get [Linker error] undefined reference to `vtable for MyDialog' and there is no moc_MyDialog.cpp generated by moc compiler.

I Use Qt 4.1.3 on windows XP and mingw - i do the build proces from QT supplied build shell. I used qmake do create make files and compile everything with a make command.

I have other classes that subclass QPushButton and QObject respectively,but they compile ok - but i can't find any fifferences between them and the broken one.

There must be missing something in the broken class.. but I'm uable to spot it :(

Help !

A: 

Please specify which tools you are using on which platform ... and what else might be interesting.

Ronny
A: 

Are you using qmake? Perhaps you didn't add it to your the .cpp file to your SOURCES and .h file to your HEADERS variable in the qmake file?

+9  A: 

The undefined reference to "vtable for MyDialog" is caused because there is no moc file. Most c++ compilers create the vtable definition in the object file containing the first virtual function. When subclassing a qt object and using the Q_OBJECT macro, this will be in the moc*.cpp file. Therefore, this error means that the moc file is missing.

The possible problems I can think of are:

  1. The header file for the class MyDialog.h is not added to HEADERS in the qmake file.

  2. You ran qmake to generate the make file before adding the Q_OBJECT macro. This created a make file without the moc rules. This is easily fixed by simply running qmake again.

  3. Your dialog derives from more than one class and QDialog is not the first class that it derives from. For qmake to work correctly, the QObject derived base class needs to be the first class that is inherited from.

David Dibben
A: 

I humbly suggest you use CMake for building QT programs on Windows. It will keep you remember to add appropriate files to it's build files.

The additional value is that you can generate make/nmake build files from it, Visual Studio solution files. And if you compile QT from sources for Visual Studio you will be able to both code and build with MS IDE/compiler.

This is of course if you are using Visual Studio at all..

Marcin Gil
A: 

The header file for the class MyDialog.h is not added to HEADERS in the qmake file.

This solves my problem, but it's not a satisfactory solution. We have a class library whose headers are in an 'include' directory. What I would like to happen is that, if a class declaration in a .h file is #included from a directory listed under the .pro file's INCLUDEPATH, the moc would run on QObject-derived classes in that header file, just as it does (apparently) when the header file is listed under HEADERS.

As I write this, it occurs to me that this might not be a good idea! Presumably we only want the header to be moc-compiled when we compile the QObject-derived class, not every time we use it. An alternative might be to have two header files, one belonging to the class and listed in HEADERS and another in the include-directory; and have one header file #include the other.

Comments?

major_tom3