I'm using Code::Blocks 8.02 and the mingw 5.1.6 compiler. I'm getting this error when I compile my Qt project:
C:\Documents and Settings\The
Fuzz\Desktop\GUI\App_interface.cpp|33|undefined
reference to `vtable for AddressBook'
File AddressBook.h:
#ifndef ADDRESSBOOK_H
#define ADDRESSBOOK_H
#include <QWidget>
class QLabel;
class QLineEdit;
class QTextEdit;
class AddressBook : public QWidget
{
Q_OBJECT
public:
AddressBook(QWidget *parent = 0);
private:
QLineEdit *nameLine;
QTextEdit *addressText;
};
#endif
File AddressBook.cpp:
#include <QtGui>
#include "addressbook.h"
AddressBook::AddressBook(QWidget *parent)
: QWidget(parent)
{
QLabel *nameLabel = new QLabel(tr("Name:"));
nameLine = new QLineEdit;
QLabel *addressLabel = new QLabel(tr("Address:"));
addressText = new QTextEdit;
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(nameLabel, 0, 0);
mainLayout->addWidget(nameLine, 0, 1);
mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
mainLayout->addWidget(addressText, 1, 1);
setLayout(mainLayout);
setWindowTitle(tr("Simple Address Book"));
}
When using Qt Creator:
Build → Run qmake
Build → Rebuild All
Warning: Do not do this if you already have a .pro file - you'll lose it!
In order to automatically ensure that all moc cpp files are generated, you can get qmake to automatically generate a .pro file for you instead of writing one yourself.
Run
qmake -project
in the project directory, and qmake will scan your directory for all C++ headers and source files to generate moc cpp files for.
The problem is almost certainly that you are not compiling or not linking in the generated moc_AddressBook.cpp file. (It should have been generated for you -- you are running Qt's moc on your code before compiling, right?)
To answer a little more thoroughly, the Q_OBJECT macro signals Qt's moc tool to create an extra implementation file that contains the code necessary to support QObject's meta-information system. If you had any signals or slots, it would do a few things for those as well.
An alternative solution might be to remove the Q_OBJECT macro. You probably don't want to do this, but it would help the immediate problem, and it isn't strictly necessary with the code that you've presented.
Also, I would note that your line:
#include "addressbook.h"
Should probably be:
#include "AddressBook.h"
based on how you presented the filenames in the question.
Assuming you are using qmake to generate your Makefile, be sure that AddressBook.h is specified in your .pro file's HEADERS's variable, e.g.
HEADERS = AddressBook.h
For CMake projects, set CMAKE_AUTOMOC to ON, this fixed my problem.
#Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
I got this while using pure virtual functions. For example,
virtual void process();
gave this error, while
virtual void process() = 0;
made it go away.
For anyone who's Googling this problem, check that all virtual functions are defined. (via " = 0" or a full definition in the source file)
I'm using Netbeans with the MinGW compiler.
I was running into the same problem using CLion, and solved it by adding these lines to CMakeLists.txt
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
See https://www.jetbrains.com/help/clion/qt-tutorial.html#qt-setup-in-clion
I had the same problem but as soon as I defined my constructor in the header file instead of the .cpp the error disappeared. Also the corresponding moc file was missing in the file system and in the Makefile section"compiler_moc_header_make_all" . I ran a qmake then finally everything built with succes. I went to check the Makefile and it was there now.
deleted the build folder, restarted Qt Creator and it worked
I come to the same problem, rebuild the project never update the Makefile, I remove the Makefile and rebuild , the the problem is gone.
ps: run 'make' from command line may give you detail information than the IDE, and helpful to get the real problem.
One cause is when you declare a virtual functions in a class and you don't define their body.
In my case Rebuild All was not enough, I had to delete build directory and make Rebuild All - then it worked!
Simply Run qmake for your project. This can easily be done by right-clicking on the name of your project and clicking on Run qmake.
Just clear the project and then rebuild it!
Go to .pro file and make sure .h file has 'include' before it.
HEADERS += include/file.h \
include/file2.h
I had the same problem trying to use a protected virtual function. Two things worked.
Changing void process(); to void process() = 0;
Making process() public instead of private
You will get the same error message if you accidentally add a destructor prototype. Add an empty destructor definition or remove the prototype.
Header files for moc compilation should contained in the HEADERS += ... variable:
I have moved the header files in the Myproject.pro to the SOURCES += ... section, because I want to have mySource.h and mySource.cpp in the same tree element. But that is faulty for QT Creator. In result the error "Undefined reference to vtable" has occured.
It seems to be:
QT detects header for moc compilation only in the HEADERS +=... section (or variable).
See also the correct explaination in the other stackoverflow answer Second anwer "I've seen a lot of ways to solve the problem, but no explanation for why it happens, so here goes.". In my mind this is an exactly explaination of the problem, which has help me to found and solve my problem.
CMake
when using CMake, interestingly enough if my .cpp and .h files are not in the same folder the moc, bu default, fails to generate the meta file :)
the most errors from using Q_OBJECT but not generate moc file
the moc file define
classname::metaObject
staticMetaObject
qt_metacall
Here is a example:
when source or header using Q_OBJECT
/opt/qt/bin/moc ./window.h -o ./moc_window.cpp
Makefile add moc_window.o in OBJS
Ex: OBJS=main.o window.o moc_window.o
try this source
https://www.mediafire.com/file/anjeah1jmm07mfe/qt_group_box.tar.gz
refer to http://fatalfeel.blogspot.com/2013/11/qt5-c-building.html
I am using Qt creator to compile and run my programs, I don't use Qt command prompt often. One thing I did to get rid of the annoying error "vtable something something" is by adding the following lines to .pro file.
TEMPLATE = app
QT += core
Related
I'm using Code::Blocks 8.02 and the mingw 5.1.6 compiler. I'm getting this error when I compile my Qt project:
C:\Documents and Settings\The
Fuzz\Desktop\GUI\App_interface.cpp|33|undefined
reference to `vtable for AddressBook'
File AddressBook.h:
#ifndef ADDRESSBOOK_H
#define ADDRESSBOOK_H
#include <QWidget>
class QLabel;
class QLineEdit;
class QTextEdit;
class AddressBook : public QWidget
{
Q_OBJECT
public:
AddressBook(QWidget *parent = 0);
private:
QLineEdit *nameLine;
QTextEdit *addressText;
};
#endif
File AddressBook.cpp:
#include <QtGui>
#include "addressbook.h"
AddressBook::AddressBook(QWidget *parent)
: QWidget(parent)
{
QLabel *nameLabel = new QLabel(tr("Name:"));
nameLine = new QLineEdit;
QLabel *addressLabel = new QLabel(tr("Address:"));
addressText = new QTextEdit;
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(nameLabel, 0, 0);
mainLayout->addWidget(nameLine, 0, 1);
mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
mainLayout->addWidget(addressText, 1, 1);
setLayout(mainLayout);
setWindowTitle(tr("Simple Address Book"));
}
When using Qt Creator:
Build → Run qmake
Build → Rebuild All
Warning: Do not do this if you already have a .pro file - you'll lose it!
In order to automatically ensure that all moc cpp files are generated, you can get qmake to automatically generate a .pro file for you instead of writing one yourself.
Run
qmake -project
in the project directory, and qmake will scan your directory for all C++ headers and source files to generate moc cpp files for.
The problem is almost certainly that you are not compiling or not linking in the generated moc_AddressBook.cpp file. (It should have been generated for you -- you are running Qt's moc on your code before compiling, right?)
To answer a little more thoroughly, the Q_OBJECT macro signals Qt's moc tool to create an extra implementation file that contains the code necessary to support QObject's meta-information system. If you had any signals or slots, it would do a few things for those as well.
An alternative solution might be to remove the Q_OBJECT macro. You probably don't want to do this, but it would help the immediate problem, and it isn't strictly necessary with the code that you've presented.
Also, I would note that your line:
#include "addressbook.h"
Should probably be:
#include "AddressBook.h"
based on how you presented the filenames in the question.
Assuming you are using qmake to generate your Makefile, be sure that AddressBook.h is specified in your .pro file's HEADERS's variable, e.g.
HEADERS = AddressBook.h
For CMake projects, set CMAKE_AUTOMOC to ON, this fixed my problem.
#Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
I got this while using pure virtual functions. For example,
virtual void process();
gave this error, while
virtual void process() = 0;
made it go away.
For anyone who's Googling this problem, check that all virtual functions are defined. (via " = 0" or a full definition in the source file)
I'm using Netbeans with the MinGW compiler.
I was running into the same problem using CLion, and solved it by adding these lines to CMakeLists.txt
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
See https://www.jetbrains.com/help/clion/qt-tutorial.html#qt-setup-in-clion
I had the same problem but as soon as I defined my constructor in the header file instead of the .cpp the error disappeared. Also the corresponding moc file was missing in the file system and in the Makefile section"compiler_moc_header_make_all" . I ran a qmake then finally everything built with succes. I went to check the Makefile and it was there now.
deleted the build folder, restarted Qt Creator and it worked
I come to the same problem, rebuild the project never update the Makefile, I remove the Makefile and rebuild , the the problem is gone.
ps: run 'make' from command line may give you detail information than the IDE, and helpful to get the real problem.
One cause is when you declare a virtual functions in a class and you don't define their body.
In my case Rebuild All was not enough, I had to delete build directory and make Rebuild All - then it worked!
Simply Run qmake for your project. This can easily be done by right-clicking on the name of your project and clicking on Run qmake.
Just clear the project and then rebuild it!
Go to .pro file and make sure .h file has 'include' before it.
HEADERS += include/file.h \
include/file2.h
I had the same problem trying to use a protected virtual function. Two things worked.
Changing void process(); to void process() = 0;
Making process() public instead of private
You will get the same error message if you accidentally add a destructor prototype. Add an empty destructor definition or remove the prototype.
Header files for moc compilation should contained in the HEADERS += ... variable:
I have moved the header files in the Myproject.pro to the SOURCES += ... section, because I want to have mySource.h and mySource.cpp in the same tree element. But that is faulty for QT Creator. In result the error "Undefined reference to vtable" has occured.
It seems to be:
QT detects header for moc compilation only in the HEADERS +=... section (or variable).
See also the correct explaination in the other stackoverflow answer Second anwer "I've seen a lot of ways to solve the problem, but no explanation for why it happens, so here goes.". In my mind this is an exactly explaination of the problem, which has help me to found and solve my problem.
CMake
when using CMake, interestingly enough if my .cpp and .h files are not in the same folder the moc, bu default, fails to generate the meta file :)
the most errors from using Q_OBJECT but not generate moc file
the moc file define
classname::metaObject
staticMetaObject
qt_metacall
Here is a example:
when source or header using Q_OBJECT
/opt/qt/bin/moc ./window.h -o ./moc_window.cpp
Makefile add moc_window.o in OBJS
Ex: OBJS=main.o window.o moc_window.o
try this source
https://www.mediafire.com/file/anjeah1jmm07mfe/qt_group_box.tar.gz
refer to http://fatalfeel.blogspot.com/2013/11/qt5-c-building.html
I am using Qt creator to compile and run my programs, I don't use Qt command prompt often. One thing I did to get rid of the annoying error "vtable something something" is by adding the following lines to .pro file.
TEMPLATE = app
QT += core
I have a legacy build that doesn't use qmake but I'd like to add QUdpSocket and connect it with a signal and a slot. I have a single class that uses Q_OBJECT. What do I need to do to properly process the Q_OBJECT directive if I want signals and slots to be available but I am not using qmake.
Can I just substitute the original myclass.h file with the output of the "$moc myclass.h"? Or is the output in addition to the original file?
Is this the likely new make directives?
m_myclass.h : myclass.h
moc myclass.h > m_myclass.h
The MOC actually generates .cpp files to be compiled along with the rest of the project; so, that would be more something like:
moc_myclass.cpp: myclass.h
moc myclass.h > moc_myclass.cpp
moc_myclass.o: moc_myclass.cpp
g++ ${CFLAGS} moc_myclass.cpp -o moc_myclass.o // whatever
and then add moc_myclass.o to the linking step of your final executable.
What is needed for errorfree compilation when adding a class that is flagged as
Q_OBJECT
? Should one run qmake file ?
New classes are written in .h .cpp files that are already added in .pro .pri files.
My metaphysics questions are
1/ is qmke_all.bat run and rebuild all enough to have Qt dependencies cleared up ? if not, what is procedure ?
2/ why is this procedure needed, what happens?
3/ what are .pro .pri for ? are both of them here for 'mapping' with qt ? should i change anything to them when adding classes but no .h .cpp files ?
I am with VS2010
thanks
If you've added Q_OBJECT to a self-defined class you need to run qmake prior to compilation, otherwise it won't generated the meta code needed for it.
1) Not sure what you mean here, sorry.
2) Or here.
3) The .pro and .pri are essentially directives files for qmake, much the same way Makefiles work for make. There's obvious differences, but you're not far off when you say they're for mapping, they're there to tell qmake how to generate the necessary compilation files. When you're adding new classes if they've not been auto-added then you need to add both the .cpp & .h files.
I inherited a class from QObject :
class Parent: public QObject
{
Q_OBJECT
QObject* cl;
public:
Parent(QObject *parent=0):QObject(parent) {
cl = NULL;
}
QObject* getCl() const {
return cl;
}
void setCl(QObject *obj) {
cl = obj;
}
};
But when I write :
Parent ev;
I get the following error:
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Parent::metaObject(void)const " (?metaObject#Parent##UBEPBUQMetaObject##XZ)
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __thiscall Parent::qt_metacast(char const *)" (?qt_metacast#Parent##UAEPAXPBD#Z)
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __thiscall Parent::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#Parent##UAEHW4Call#QMetaObject##HPAPAX#Z)
You should delete the debug folder of your application and run it again to correct this problem.
If you're using Visual Studio, delete the line Q_OBJECT from the header file, save the file, put Q_OBJECT back into the header file, save the file again. This should generate the moc_* file and should build and link correctly.
I noticed some answers are based on Visual Studio.
This answer is based on Qt Creator.
Unlike the name suggest, Rebuild Project will not wipe out everything and build from scratch. If you recently added QObject (and/or Q_OBJECT) to your class, you'll have to run qmake again, e.g.
Clean Project
Run qmake
Build Project
This is because, by default, qmake only runs when you do significant changes to your solution like adding new source files or modify the .pro file. If you make edits to an existing file, it doesn't know it needs to run qmake.
As a fall back, to brute force Qt to build everything from scratch, delete the Debug or Release folder.
So the issue was I needed the Qt MOC compiler to compile my .h file. This is required for any classes that extend QObject or one of its children. The fix involed (for me) right-clicking on the header file, choosing Properties, and setting the Item Type to "Qt MOC Input", then hitting "Compile" on the header, and then adding the resulting moc_myfilename.cpp file to my project.
I added cpp/ui files to my project manually, but forgot to add the header file explicitly as header file. Now when compiling I got a similar error message as above and the moc_*.cpp file(s) were not generated in the debug (or release) directory of the build. That was not such an obvious mistake, qmake did not complain and other than the linker message I got no errors.
So if anyone encounters the same problem again (or makes the same copy & pase mistake): make sure the header files have also been added to your project file
If your moc files are generated in the visual studio project try to include them into project if they are not included into project then rebuild.
I had the same problem in Visual Studio, and solved it by taking the following steps:
Right-click header file in solution Explorer
Properties
Change "Item Type" to "Custom Build Tool"
Then in the Custom Build Tool configuration:
Go to General
set "Command Line" to:
"$(QTDIR)\bin\moc.exe" "%(FullPath)" -o ".\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp" "-fStdAfx.h" "-f../../../src/FileName.h" -DUNICODE -DWIN32 -DWIN64 -DQT_DLL -DQT_NO_DEBUG -DNDEBUG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_WIDGETS_LIB -DQT_NETWORK_LIB -DWIN32_LEAN_AND_MEAN -DDIS_VERSION=7 -D_MATH_DEFINES_DEFINED "-I.\SFML_STATIC" "-I.\GeneratedFiles" "-I." "-I$(QTDIR)\include" "-I.\GeneratedFiles\$(ConfigurationName)." "-I$(QTDIR)\include\QtCore" "-I$(QTDIR)\include\QtGui" "-I$(QTDIR)\include\QtNetwork"
set "Outputs" to:
.\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp
set "Additional Dependencies" to:
$(QTDIR)\bin\moc.exe;%(FullPath)
Your exact values may be different. They are usually applied via a Qt plugin.
I had this problem with Visual Studio 2012 when I had a Q_OBJECT class definition in my cpp file. Moving the class definition to the header file resolved the issue.
It looks like it should be possible to support Q_OBJECT class in cpp file by adding the cpp file to moc but I did not try this.
I use CMake to manage Qt projects and the new Q_OBJECT needs to be added under the QT4_WRAP_CPP call. This will generate the moc_*.cxx for inclusion in the project and clean up the unresolved externals.
My problem was that one of my files that used a Qt macro didn't get moc'ed. I found out, that the Qt Plugin for Visual Studio doesn't recognize the Q_NAMESPACE macro and therefore doesn't add the file to the moc'ing list.
So I used the solution from this answer to add the file to the mic'ing list:
You should find a .h file which has successfully generated
"moc_*", and copy all the contents in "Custom Build Tool -> General"
to the new .h file setting page.
Be careful with the different options for Debug and Release-Mode.
After that, build your project.
Build it once each in Debug and Release-Mode
Finally, add the generated "moc_*" file to your project.
Now, "moc_filename.cpp" should be in Generated Files\Debug and Generated Files\Release.
Right click on each of them and change thair properties:
The file in Debug: Change configuration to Release and then change General->Excluded from build to yes.
The file in Release: Change configuration to Debug and then change General->Excluded from build to yes.
Faced this issue in case of chained CMake targets. Turned out I had to enable CMAKE_AUTOMOC even in targets that didn't use Qt directly (transitively). Also turned out that CMAKE_AUTOMOC can't be used w/o find_package(QtX) in same CMakeLists.txt or CMakeLists.txt of parent.
In my case (using QtAdd-in with VS2012 and Qt v4.8.4) none of the above suggestions worked.
For some reason VS could not generate proper moc files (build output: No relevant classes found. No output generated.) and when I compiled relevant headers by hand (setting qt moc as a compiler and clicking 'Compile') it produced empty moc file.
What did work was to compile all necessary mocs from command line (moc -o moc_SomeClass.cpp SomeClass.h) and then replace the wrong ones in GeneratedFiles folder.
This is only workaround (and not a convenient one for a big project) to have your project build succesfully, but does not really explain strange VS/QtAdd-in behaviour.
Using QtAdd-in with VS2010 i realized the moc_*.cpp files were updated in the GeneratedFiles/Debug folder although i was in release mode. Copying the files into the Release folder worked for me.
I know that this is a very old question, but it seems to be still interesting (I've been here at least 4 or 5 times in the last months) and seems like I found another reason for which is possible to get this error.
In my case in the header file I wrongly typed:
#include "MyClass.h""
Only after inspecting the whole output I found out that at that line the compiler was emitting a warning.
Now that I removed the additional quotation mark my QObject compiles perfectly!
I've encountered this problem with the use of a "private class" in Qt when employing the "PIMPL" (private implementation) programming pattern. Qt uses this model all through out their source code. I have come to really like it myself.
This technique involves the use of a "private" forward-declared class in a public header file, which will be be used by the "public" class (i.e. it's "parent"). The parent then has a pointer to an instance of the private class as a data member.
The "private" class is defined entirely within the cpp file for the public one. There is NO header file for the private class.
All of the "dirty work" is done with that private class. This hides all of the implementation of your public class including every other private member typically (both data and functions).
I highly recommend learning about the PIMPL pattern - especially if you are going ever read the internal Qt source.
Without explaining that coding style further, here's the point as it relates to this question... To get the Q_OBJECT macro to work inside the cpp for the "private" class to be QObject which can use signals/slot etc., you needed to explicitly include the .moc to the public class inside the cpp:
#include "MyPublicClass.moc"
You can ignore any IDE warnings about this line.
I'm not sure if it matters exactly off hand, but that inclusion I always see done AFTER the private class definition, rather than at the top of the cpp (like includes are typically placed). So, the cpp layout goes like this:
"Normal" includes are defined.
The private class is defined.
The moc for the public class is #included.
The public class implementation is defined.
Visual Studio 2017.
I've added file to already set up Qt project and got this error. How I fixed it:
Right click on the header in Solution Explorer
Properties... -> Configuration Properties -> General -> Item Type
Change from C/C++ Header to Qt Meta-Object Compiler (moc)
voila :)
This happened to me recently when switching from MingW to MSVC. I had a prototyped class/struct listed as a class, and MingW didn't mind.
MSVC definitely sees a difference between class and struct when prototyping is concerned.
Hope that helps someone else one day.
In my case, none of the above worked but it was totally my mistake.
I had overrided virtual functions in .h file (declared them) but had never defined them in .cpp :)
Either answer works for me in the VS 2013 environment. I eventually solve the problem by removing the .h/.cpp from project, and adding it back.
I am working in VS2015 with an integrated Perforce p4v client.
In my case Perforce tried to add moc file to a depo, when I reverted this operation, Perforce removed this moc file from project and deleted it.
The file was recreated after the next compilation, but it wasn't included in project, I have to add it manually to Generated files, when I finally understood what was the problem.
I have the same problem, my solution was the encoding( my file with "UTF16LE BOM" can't generate with moc.exe ) , y create another file with ASCII enconding and it work.
HxD HexEditor can help you to see the codification.
For me, this is the cause: some header or source file not included in QT's project file
In my case I had the .h and the .cpp file for the problematic QObject ancestor in subfolders of the project. When I moved them next to the CMakeLists.txt (project root folder) it linked successfully. I'm probably missing some CMake command to include mocs fot files in subdirectories.
when I removed Q_OBJECT it works fine.
I'm using Clion + CMake + MSVC/14.31.31103.
Is it because Q_OBJECT no longer needed after these?
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
For Visual Studio 2022 (with Qt VS Tools extention)
In Solution Explorer right click on the affected header file and select Properties.
Under "Configurations Properties -> General -> Item Type" select the option "Qt Meta-Object Compiler (moc)".
Then rebuild the project.
In Qt Creator
It turned out I had this error because I added Q_OBJECT afterwards in the .h file.
I managed to fix it after removing and adding the file back into the project.
Happy coding...
I solved my problem by adding this to my header file :
#ifndef MYCLASSNAME_H
#define MYCLASSNAME_H
... // all the header file content.
#endif
I'm developing a simple Qt 4 app and making my own dialog. I subclassed QDialog, inserted the Q_OBJECT macro in the class declaration block, and... I get
[Linker error] undefined reference to `vtable for MyDialog' and there is no
moc_MyDialog.cpp generated by the moc compiler.
I am using Qt 4.1.3 on Windows XP and mingw. I followed the build process from the Qt-supplied build shell. I used qmake to create make files and compiled everything with a make command.
I have other classes that subclass QPushButton and QObject respectively, but they compile OK. I can't find any differences between them and the broken one.
There must be missing something in the broken class, but I'm unable to spot it.
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:
The header file for the class MyDialog.h is not added to HEADERS in the qmake file.
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.
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.
If you are using Qt Creator, you might get this error if your previous deployment was failed due to some reason (like application already running). In that case, simply do a 'Clean Project' and then 'Rebuild Project' and then 'Run' to deploy.
If you have your header file included, follow the steps:
Right click on the project where you have added this.
Hit 'Run qmake'.
This will will clear up the old references and build with th Q_OBJECT macro. QT doesn't do it on rebuild.
I have see that the problem appear only when is added a class with no extension and then an extension is added manually.
To solve the problem i put Q_OBJECT in the .h of the class and then right click on "Sources" -> "Add Existing Files..." selecting the .ccp of my modified class.
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?
I humbly suggest you use CMake for building Qt programs on Windows. It will keep you remember to add appropriate files to its 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 source 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.
The message undefined reference to `vtable for MyDialog' can also be the result of a missing implementation (in MyDialog) of a pure virtual function in a class that MyDialog is derived from.