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!
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!