I have written a simple signal slot application using Qt. I want to send a signal to another thread that is runs out of the main thread.
Here is my code:
class Thread1 : public QThread
{
Q_OBJECT
void run()
{
exec();
}
public:
Thread1(QObject* parent);
public slots:
void a()
{
qInfo()<<QThread::currentThreadId();
}
};
class Object : public QObject
{
Q_OBJECT
public:
Object(){}
void start()
{
qInfo()<<QThread::currentThreadId();
Thread1* thread = new Thread1(this);
connect(this,SIGNAL(a()),thread,SLOT(a()));
thread->start();
emit a();
}
signals:
void a();
};
But it returns:
0x7f9851c988c0
0x7f9851c988c0
How can I call a signal that outputs another threadID?
Implement
Thread1
constructor like following :You've got it backwards. A
QThread
is a thread handle, not a thread itself. If you want to run something in another thread, it belongs in a plainQObject
that you move to a thread. You don't need to derive fromQThread
at all! You also shouldn't move aQThread
's baseQObject
to the thread itself. What you do is have a handle to the thread live in the thread itself. As soon as the thread finishes, the handle becomes non-functional (aQObject
with a nullthread()
).First of all, if all you need is to run some code that runs to completion (e.g. does a calculation) in a worker thread, use the thread pool and
QtConcurrent
framework. It manages all the threads for you:If you insist on controlling the thread's lifetime yourself, you'd do the following:
Finally, note that the
QDebug
class knows how to output the object's address, class and name (if set) when passed a pointer to aQObject
. Thus, you don't need to useQThread::currentThreadId()
, theQThread::currentThread()
is sufficient - and you can give the threads mnemonic names since they areQObject
s, after all.