Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to signal in QT

I wanted to create a class in an separate file in Qt and then use this class in my main file (Background: Secondary thread updating GUI). Thus I wrote ReadDPC.h-file:

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

And in my ReadDPC.cpp-file:

void ReadDPC::run()
{
    while(1)
    {
        usleep(50);
        int counts = read_DPC();
        emit currentCount(counts);
    }
}

read_DPC() is a function returning an int-value also placed in the cpp-file.
But when I want to compile this, I get the error undefined reference to ReadDPC::currentCount(int). Why? How can I solve this?

Edit: Added Q_Object-Macro, no solution.

like image 355
arc_lupus Avatar asked Oct 15 '14 14:10

arc_lupus


Video Answer


2 Answers

Add Q_OBJECT macro to your subclass and run qmake.

This macro allows you use signals and slots mechanism. Without this macro moc can't create your signal so you get error that your signal is not exist.

Code should be:

class ReadDPC: public QThread {
Q_OBJECT

Note that when you use new signal and slot syntax, you can get compile time error that you forgot add this macro. If it is interesting for you, read more here: http://qt-project.org/wiki/New_Signal_Slot_Syntax

like image 100
Kosovan Avatar answered Oct 05 '22 03:10

Kosovan


  1. add Q_OBJECT
  2. Clear your project
  3. Run qmake
  4. And only after that, run your project
like image 37
K.Alex Avatar answered Oct 05 '22 05:10

K.Alex