QThread Signal Not Received By Main Thread Slot
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"
Code:
#include <QtGui>
{
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"
Code:
#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!
Re: QThread Signal Not Received By Main Thread Slot
What happens if you remove Qt::DirectConnection ?
Re: QThread Signal Not Received By Main Thread Slot
You can't use DirectConnection with threads, they must be queued.
Re: QThread Signal Not Received By Main Thread Slot
No change, unfortunately.
Re: QThread Signal Not Received By Main Thread Slot
Where do you run the event loop for the thread?
Re: QThread Signal Not Received By Main Thread Slot
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...
Re: QThread Signal Not Received By Main Thread Slot
It shouldn't be necessary tbh, just a stab in the dark.
What happens when you step through the run method?
Re: QThread Signal Not Received By Main Thread Slot
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).