问题
I recently needed to add a signal to a class, so I changed the class to inherit from QObject and added the Q_OBJECT macro into the class definition. Since doing so I get "signal undefined reference error for 'vtable for CLICommand'" error on the class line below:
// File clicommand.h
#include <QString>
#include <QStringList>
#include <QTcpSocket>
#include "telnetthread.h"
class CLICommand : public QObject
{
Q_OBJECT
public:
CLICommand(TelnetThread *parentTelnetThread);
signals:
void signal_shutdown_request();
private:
TelnetThread *m_parentTelnetThread;
and the second error "signal undefined reference error for 'vtable for CLICommand'" on the line below (intializing the member variable):
// File clicommand.cpp
#include <QDebug>
#include <QTcpSocket>
#include <QTextStream>
#include "version.h"
#include "clicommand.h"
#include "telnetthread.h"
#include "logger.h"
CLICommand::CLICommand(TelnetThread *parentTelnetThread)
: m_parentTelnetThread(parentTelnetThread)
{
}
and just here is where I emit the signal. The emit line generates error undefined reference to `CLICommand::signal_shutdown_request()' :
// file shutdown_clicommand.cpp
#include <QIODevice>
#include "clicommand.h"
#include "logger.h"
#include "version.h"
void CLICommand::execute_shutdown(const QStringList &commandLineFragments)
{
emit signal_shutdown_request();
}
I've read a bunch of posts on this topic but none seem to apply. I even tried clean/rebuildall. I am not using boost or other libraries...just QT 5
Can someone tell me what I'm doing wrong?
SOLUTION: in QT Creator, right click the project, select RUN QMAKE, then rebuild all. Other posts about running REBUILD ALL are incorrect...on it's own that will NOT run qmake.
回答1:
You need to make sure 'moc' is (re)run for those changes. It seems that you either miss the generated moc file, or that is outdated and hence not containing the proper reference to the current state.
QtCreator does not rerun qmake
properly when the Q_OBJECT macro is added. This has been a long standing issue, so you will need to rerun it manually.
https://bugreports.qt.io/browse/QTCREATORBUG-231
来源:https://stackoverflow.com/questions/19173068/qt-signal-undefined-reference-error-after-inheriting-from-qobject