PDA

View Full Version : QThread Signal Not Received By Main Thread Slot



EF2008
3rd June 2010, 21:21
I'm having a rather strange problem, and I'm wondering if anyone can help set me on track.

I'm new to working with QThreads, and I'm quite sure that this is a logic problem more than anything else.

I can't seem to connect my thread's custom signal to the desired slot on the main thread that launched it.

"mythread.h"


#include <QtGui>
class MyThread: public QThread
{
Q_OBJECT
public:
MyThread(int thread_id)
{
connect(this, SIGNAL(threadFinished(int)), this, SLOT(finished(int)),Qt::DirectConnection);
}
~MyThread();

signals:
void threadFinished(int thread_id);

public slots:
void finished(int)
{
qDebug() << "finished(int) received in thread"; // output displays this
}

protected:
void run()
{
runGeneralQuery();
runProgramNameQuery();

emit threadFinished(thread_id);
}

private:
void runGeneralQuery(){};
void runProgramNameQuery(){};

};


"threadmanager.h"


#include <QtGui>
#include "mythread.h"

class ThreadManager: public QObject
{
Q_OBJECT
public:
ThreadManager()
{
/*...*/
runThreads();
}


void runThreads()
{
thread= new MyThread(0);
connect(thread, SIGNAL(threadFinished(int)), this, SLOT(finished(int)), Qt::DirectConnection);

thread->start();

}


public slots:
void finished(int thread_id)
{
qDebug() << "finished(int) received in manager"; // output does not display this
}

private:
MyThread* thread;

};



Does anyone have any suggestions as to why the signal isn't being received by the slot on the main thread?

Thanks in advance!

tbscope
3rd June 2010, 22:03
What happens if you remove Qt::DirectConnection ?

squidge
3rd June 2010, 22:05
You can't use DirectConnection with threads, they must be queued.

EF2008
3rd June 2010, 22:05
No change, unfortunately.

squidge
3rd June 2010, 23:08
Where do you run the event loop for the thread?

EF2008
4th June 2010, 00:30
Is executing the thread's event loop necessary for what I'm doing? I just need work to be done in MyThread::run(), have it emit a signal, and return...

squidge
4th June 2010, 08:59
It shouldn't be necessary tbh, just a stab in the dark.

What happens when you step through the run method?

wysota
4th June 2010, 09:06
Let's make one thing clear - you are not making connections across threads. QThread objects live in the main thread (at least in your situation).