Q_OBJECT throwing ‘undefined reference to vtable’ error [duplicate]

It is because the unit generated by MOC isn’t included in the linking process. Or maybe it isn’t generated at all. The first thing I’d do is to put the class declaration in a separate header file, perhaps the build system isn’t scanning implementation files.

Another possibility is that the class in question once didn’t belong to Qt meta object system (that is, it had no Q_OBJECT or maybe didn’t inherit from QObject at all), so qmake needs to be run again in order to create the necessary rules for MOC. The easiest way to force qmake to be run is to make some insignificant changes to the project file to update its timestamp, like adding and then removing some white space. Or, if you’re using Qt Creator, then just select “Run qmake” from the project context menu.

Leave a Comment