[Qt-interest] Warning: QObject::startTimer: timers cannot be started from another thread

Sean Harmer sean.harmer at maps-technology.com
Thu Mar 18 21:03:59 CET 2010


Hi,

Eirik Ulvik wrote:
> I am in the process of making our application multithreaded. The problem
> I am now facing is the error message in the subject is printed all the
> time while executing the program. I also get the same sort of message
> for QObject::killTimer. After googling a bit I found, as expected, that
> this is related to doing GUI operation in threads other than the main Qt
> GUI thread.
> 
> My next step is to strip all GUI operations from the code that runs in a
> separate thread. As much of this code is quite old and did not separate
> GUI code from other code, locating the code in question is difficult. So
> my question to the list is if anyone has had experience in a similar
> situation or have some general pointers on good ways of pin pointing GUI
> code I would be very grateful.

Do either:

(1) Set break points on all occurrances of startTimer()
(2) Set break point in QObject::startTimer() and look at the stack to 
see where it is being called in your code.
(3) export the Q_FATAL_WARNINGS environment variable and run your app in 
a debugger. It will abort whenever Qt issues a warning. Again look at 
the stack to see where in your code it is being triggered.

Once you find where it is being called, find from which thread's context 
it is being called and fix it ;-) Once you find the problem spots and 
can make a small compileable example for each type of problem post it to 
the list and we can tell you how to resolve them.

A good approach when using QObjects in worker threads is to not make 
your QThread subclass do the work. This is because the thread object 
itself has affinity with the thread that created it - usually the main 
GUI thread. Instead, create a QObject derived object in the QThread's 
run() function and have that do the work instead. That way, your QObject 
will have affinity with the worker thread and not the main thread.

Good luck,

Sean



More information about the Qt-interest-old mailing list