Solved QThreads problem: QObject::startTimer: Timers cannot be started from another thread
-
Hi,
I have the following problem: I created a QObject called Controller which runs in its own thread. This "Controller" has a member QObject called "Worker" which contains a QTimer. When I try to start the QTimer inside a function of worker, I get the error message
QObject::startTimer: Timers cannot be started from another thread
However, when in the cnstructor of Worker I set the parent QObject to "Controller" I don't get this message. I don't understand why this is.
-
Ok, the relevant documentation for this is this:
Note: A QObject's member variables do not automatically become its children. The parent-child relationship must be set by either passing a pointer to the child's constructor, or by calling setParent(). Without this step, the object's member variables will remain in the old thread when moveToThread() is called.
The problem was that I used moveToThread on "Controller" but the child "Worker" would remain in the old thread if it is not a QObject child.