QT中信号的未定义参考

arc*_*pus 3 c++ qt multithreading

我想在Qt中的单独文件中创建一个类,然后在我的主文件中使用此类(后台:辅助线程更新GUI).因此我写了ReadDPC.h-file:

class ReadDPC: public QThread
{
//First edit:
Q_OBJECT
//End of first edit
public:
    void run();
signals:
    void currentCount(int);
};
Run Code Online (Sandbox Code Playgroud)

在我的ReadDPC.cpp文件中:

void ReadDPC::run()
{
    while(1)
    {
        usleep(50);
        int counts = read_DPC();
        emit currentCount(counts);
    }
}
Run Code Online (Sandbox Code Playgroud)

read_DPC()是一个返回int-value 的函数,也放在cpp文件中.
但是当我想编译它时,我得到了错误undefined reference to ReadDPC::currentCount(int).为什么?我怎么解决这个问题?

编辑:添加Q_Object-Macro,没有解决方案.

Che*_*byl 10

将Q_OBJECT宏添加到子类并运行qmake.

此宏允许您使用信号和插槽机制.如果没有这个宏,moc就无法创建信号,因此您会收到信号不存在的错误.

代码应该是:

class ReadDPC: public QThread {
Q_OBJECT
Run Code Online (Sandbox Code Playgroud)

请注意,当您使用新的信号和插槽语法时,您可能会遇到编译时错误,您忘记添加此宏.如果您对此感兴趣,请在此处阅读更多内容:http://qt-project.org/wiki/New_Signal_Slot_Syntax

  • @arc_lupus同样的问题:操作只需运行qmake:http://stackoverflow.com/questions/25470635/qconnect-find-no-such-slot-on-qcombobox-by-qt-creater/25470865#25470865 (3认同)

K.A*_*lex 7

  1. 添加Q_OBJECT
  2. 清除您的项目
  3. 运行qmake
  4. 然后才运行你的项目