[Qt-interest] Timer Events Execute in Wrong Thread

Josiah Bryan jbryan at productiveconcepts.com
Mon Jul 19 15:10:36 CEST 2010


Odd problem here - and it might just be me not understanding how to use 
QTimers in a QThread. Anyway, using a QThread here with a QTimer to 
generate signals every 30th of a second. In the "real" program, 
sometimes the processing to generate the signal can take a while 
(upwards of a second or more) - hence putting it in a QThread to not 
freeze the main app event loop, right?

Well, all good in theory. However, the attached test program shows that 
while the QThread::run() method is indeed executed in a separate thread, 
the slot the timer is connected to on the thread object 
(TestThread::tick(), private slot), executes in the main thread 
(according to QThread::currentThreadId()).

Here's a sample output from the attached test program:

CameraTest::ctor: Main thread ID  3086677712
TestThread::run: Test thread ID  3084352400
CameraTest::paintEvent: In ID  3086677712
CameraTest::frameReady: In ID  3086677712
...
TestThread::tick: In ID  3086677712


Notice the thread ID that tick() is in is the main thread ID as reported 
at the start, not the test thread's ID.

To test things out to make sure it really was in the main loop, I put a 
50 second sleep in TestThread::tick() - which halted the main event loop 
(the timer in the main event loop stops firing and resizes of the window 
didn't trigger a repaint of the widget.)

I've tested this on two separate machines / OS versions. One machine: 
CentOS 5.5 / Qt 4.6.3, and 2nd machine: FC8 / Qt 4.6.2, both with same 
results on both machines.

Am I just using the QThread/QTimers wrong or not setting something up 
correctly? Or is this really a "bug" of sorts?

Thanks for any help you can offer!
Cheers!
-Josiah

-- 

-=-=-=-=-=-=-=-=-=-=-=-=-
Josiah Bryan
Productive Concepts, Inc.
jbryan at pciint.com
(765) 964-6009, ext. 224
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: CameraTest.h
Url: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100719/097739a4/attachment.h 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.cpp
Type: text/x-c++src
Size: 196 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100719/097739a4/attachment.bin 


More information about the Qt-interest-old mailing list