
Originally Posted by
Cruz
in #else there is just the thread sending signals to itself.
No, that's not true. The timer lives in the main thread and the "Object" lives in the worker thread.
Having tried it this way I can confirm that it works. My thread receives signals from main and signals from the thread to main are also received and I never called exec().
It means you don't have cross-thread signals
The "Thread" object from my example and all its members live in the main thread. You can easily verify what I say, just add the following call to your main() and to the slot you assume works in a different thread and then compare the two values:
qDebug
() <<
QThread::currentThreadId();
qDebug() << QThread::currentThreadId();
To copy to clipboard, switch view to plain text mode
You'll see that in your case they are the same and in my case they are different.
In my example we get:
$ ./ev
App thread: 140623696955232
void Object::someSlot()
Object thread: 140623509120768
$ ./ev
App thread: 140623696955232
void Object::someSlot()
Object thread: 140623509120768
To copy to clipboard, switch view to plain text mode
Here is a complete test code:
#include <QtGui>
Q_OBJECT
public slots:
void someSlot() {
qDebug() << Q_FUNC_INFO;
qDebug
() <<
"Object thread:" <<
QThread::currentThreadId();
}
};
#include "main.moc"
public:
Thread() {
timer.start(1000);
qDebug
() <<
"Thread thread:" <<
QThread::currentThreadId();
}
void run() {
Object o;
connect(&timer, SIGNAL(timeout()), &o, SLOT(someSlot()));
qDebug
() <<
"This is thread:" <<
QThread::currentThreadId();
qDebug() << "Does timer live in me? " << (timer.thread()==this ? "Yes" : "No");
qDebug() << "Does Object live in me? " << (o.thread()==this ? "Yes" : "No");
forever {
; // do nothing
}
}
private:
};
int main(int argc, char **argv){
qDebug
() <<
"App thread:" <<
QThread::currentThreadId();
#if 1
Object o;
QObject::connect(&timer,
SIGNAL(timeout
()),
&o,
SLOT(someSlot
()));
thread.start();
o.moveToThread(&thread);
timer.start(1000);
#else
Thread thread;
thread.start();
#endif
return app.exec();
}
#include <QtGui>
class Object : public QObject {
Q_OBJECT
public slots:
void someSlot() {
qDebug() << Q_FUNC_INFO;
qDebug() << "Object thread:" << QThread::currentThreadId();
}
};
#include "main.moc"
class Thread : public QThread {
public:
Thread() {
timer.start(1000);
qDebug() << "Thread thread:" << QThread::currentThreadId();
}
void run() {
Object o;
connect(&timer, SIGNAL(timeout()), &o, SLOT(someSlot()));
qDebug() << "This is thread:" << QThread::currentThreadId();
qDebug() << "Does timer live in me? " << (timer.thread()==this ? "Yes" : "No");
qDebug() << "Does Object live in me? " << (o.thread()==this ? "Yes" : "No");
forever {
; // do nothing
}
}
private:
QTimer timer;
};
int main(int argc, char **argv){
QApplication app(argc, argv);
qDebug() << "App thread:" << QThread::currentThreadId();
#if 1
QTimer timer;
Object o;
QObject::connect(&timer, SIGNAL(timeout()), &o, SLOT(someSlot()));
QThread thread;
thread.start();
o.moveToThread(&thread);
timer.start(1000);
#else
Thread thread;
thread.start();
#endif
return app.exec();
}
To copy to clipboard, switch view to plain text mode
And output in the second case:
App thread: 140557882951520
Thread thread: 140557882951520
This is thread: 140557695117056
Does timer live in me? No
Does Object live in me? Yes
App thread: 140557882951520
Thread thread: 140557882951520
This is thread: 140557695117056
Does timer live in me? No
Does Object live in me? Yes
To copy to clipboard, switch view to plain text mode
Bookmarks