arc_lupus arc_lupus -4 years ago 242
C++ Question

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.

Answer Source

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 QThrea{
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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download