Today, I read the multi-thread section in the QT article of 1 + 1 = 2 Daniel, so I want to implement it myself. I didn't expect this problem undefinedreferenceto 'vtable ...., I checked a lot of information for a long time and found it difficult to understand the information. For more information, see http://www.examda.com/ncre2/cpp/fudao/20081219/085036477.html.
This error occurs after the qobject keyword is added to the program, but I suddenly remembered that when I was a beginner at QT, why haven't I met the second edition of the Official textbook when I wrote the program? Is it because I wrote all the code to main. why in CPP? Well! That's it!
The entire solution process is as follows:
First, I used QT to create the project test: add the code in Main. cpp as follows:
#include <QCoreApplication>#include <QThread>#include <QDebug>#include <QObject>class LongTimeAct: public QObject{ Q_OBJECTpublic slots: void act();};class Begin:public QObject{ Q_OBJECTsignals: void sig();public: void emiting();};void LongTimeAct::act(){ qDebug()<<"slot---My thread id is :"<<QThread::currentThreadId();}void Begin::emiting(){ qDebug()<<"sig---my thread id is :"<<QThread::currentThreadId(); emit sig();}int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); qDebug()<<"main---my thread id is :"<<QThread::currentThreadId(); QThread thread; LongTimeAct act; Begin begin; act.moveToThread(&thread); QObject::connect(&begin,SIGNAL(sig()),&act,SLOT(act())); thread.start(); begin.emiting(); return a.exec();}
After compilation, the error in the question occurs, for example:
So I added a test. h file and a test. cpp file to the project. Split the code in Main. cpp into these two files.
The code in test. H is as follows:
#ifndef TEST_H#define TEST_H#include <QObject>class LongTimeAct: public QObject{ Q_OBJECTpublic slots: void act();};class Begin:public QObject{ Q_OBJECTsignals: void sig();public: void emiting();};#endif // TEST_H
The code in test. cpp is as follows:
#include <QThread>#include <QDebug>#include "test.h"void LongTimeAct::act(){ qDebug()<<"slot---My thread id is :"<<QThread::currentThreadId();}void Begin::emiting(){ qDebug()<<"sig---my thread id is :"<<QThread::currentThreadId(); emit sig();}
The code in Main. cpp is as follows:
#include <QCoreApplication>#include <QDebug>#include <QThread>#include "test.h"int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); qDebug()<<"main---my thread id is :"<<QThread::currentThreadId(); QThread thread; LongTimeAct act; Begin begin; act.moveToThread(&thread); QObject::connect(&begin,SIGNAL(sig()),&act,SLOT(act())); thread.start(); begin.emiting(); return a.exec();}
Compilation succeeds !!!! For example:
Unfortunately, I only solved this compilation error, but I don't know why, because I checked it. pro file, found that in addition to adding the header file and CPP file, there is no difference with the original.
Hope that the passing Daniel can tell you why this compilation error can be avoided. Thank you!
Another point is that I first metCollect2: LD returned 1 exit status error, because I am not familiar with the syntax. I have defined the SIG () function in the begin class to output a sentence when sending a signal. This function is a signal function, you can only declare that it cannot be defined by yourself. QT will process it internally. For details about the signal and slot, see http://blog.csdn.net/michealtx/article/details/6858784. Put the sentence output in the emiting () function that emits the SIG () signal.
SIG (); previously, see the emiting () function in test. cpp,The following code is incorrect:
void Begin::sig(){ qDebug()<<"sig---my thread id is :"<<QThread::currentThreadId();}