PDA

View Full Version : threads, signals and slots



cyclic
2nd September 2009, 08:40
hi,

im trying to use a thread to do some background work. It takes a bunch of messages and checks the status of each until it has a "finished" status.

messagestatusthread.h


class MessageStatusThread : public QThread
{
public:
MessageStatusThread(QList<Message*> *messages);
void run();

private:
QList<Message*> *m_messages;
QMultiMap<QString, Address*> *unfinishedMesasgeStatusMap;

QString* buildXml();
void parseXml(QString data);

public slots:
void messageStatusFinished(QString);
};

messagestatusthread.cpp run method


void MessageStatusThread::run()
{
while(!unfinishedMesasgeStatusMap->isEmpty()) {

CommunicationFacade *communication = new CommunicationFacade();
connect(communication, SIGNAL(communicationFinished(QString)), this, SLOT(messageStatusFinished(QString)));

communication->sendRequest(buildXml());

sleep(30);
}

}

I'm using the CommunicationFacade in other parts of the program but not in a separate thread and there it works.

I get Object::connect: No such slot QThread::messageStatusFinished(QString) in messagestatusthread.cpp:34 when I run the program.

The messageStatusFinished(QString) slot is implemented in messagestatusthread.cpp.

Can somebody point out what I'm doing wrong?

Thanks

victor.fernandez
2nd September 2009, 08:42
You forgot to add the Q_OBJECT macro in the class definition.

cyclic
2nd September 2009, 08:48
when I add that I get:

/src/messagestatusthread.cpp:13: undefined reference to `vtable for MessageStatusThread'

wysota
2nd September 2009, 08:50
Rerun qmake afterwards.

cyclic
2nd September 2009, 09:31
thanks, that helped

but now things get really strange.
Now i don't get any errors but the slot is not executed, nothing happens

spirit
2nd September 2009, 09:35
you should add exec (for enabling event loop in a thread) in you run method if you need process signal in you thread.

cyclic
2nd September 2009, 09:44
thank you all!

now it works