How to move QTimer from GUI thread?
-
Hey, how can I move QTimer from GUI thread? When I'm printing currentThreadId() from run(), I got diffrent threadId than calling currentThreadId() from main(). But when printing from loop() I got the same threadId like from main().
My current code: https://github.com/fire7d/TestProject -
@BD9a
Hi
Docs says
"It is important to remember that a QThread instance lives in the old thread that instantiated it, not in the new thread that calls run(). This means that all of QThread's queued slots and invoked methods will execute in the old thread. Thus, a developer who wishes to invoke slots in the new thread must use the worker-object approach; " -
As always - please read the QThread documentation and don't use threads when you did not understood it.
Your Printer object (= QThread object) lives in the main thread. Only stuff inside run() is in the newley created thread. Therefore your slot is executed in the main thread. -
@BD9a said in How to move QTimer from GUI thread?:
So I have to add moveToThread(this); in constructor? Everything else is fine?
No, you should follow the examples in the documentation. Or remove the threading completely - it's not needed in your code at all.
-
@Christian-Ehrlicher In this code it's not needed, but I want to learn on this one instead of messing in other projects
-
@BD9a
Hi
Docs says
"It is important to remember that a QThread instance lives in the old thread that instantiated it, not in the new thread that calls run(). This means that all of QThread's queued slots and invoked methods will execute in the old thread. Thus, a developer who wishes to invoke slots in the new thread must use the worker-object approach; "