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>
{
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(){};
};
#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(){};
};
To copy to clipboard, switch view to plain text mode
"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;
};
#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;
};
To copy to clipboard, switch view to plain text mode
Does anyone have any suggestions as to why the signal isn't being received by the slot on the main thread?
Thanks in advance!
Bookmarks