[Qt-interest] QThread event loop running in main thread?

BRM bm_witness at yahoo.com
Mon Dec 27 17:51:51 CET 2010


I'm taking a guess - but his probably doesn't work b/c it's a SLOT in the 
QThread object that is being connected to, and therefore not properly crossing 
the thread boundary.

I do agree that some better QThread examples are necessary - especially with the 
way things have changed since Qt3. (I didn't do anything for Qt3 but there are a 
lot of Qt3 based examples out there!)

Thus far, I've done:

- Derived from QThread but only used signals to transfer to an internal object 
that runs only in the new thread and is not visible any where else.
- Used QThread without modification, but found I had to run moveToThread() 
before connecting the signals/slots to get it to work - or at least that's what 
it seemed like (Qt 4.5.1).

Two very different models, but neither have any good examples in the Qt 
documentation. That said, I have come across several very good blog posts on Qt 
Labs per QThreading - one of which is what converted me to start using the 
second model above (no QThread derivatives), though I can't seem to find it 
right now.

Ben



>
>From: Hannu Shemeikka <hps at shemeikka.org>
>To: qt-interest at qt.nokia.com
>Sent: Mon, December 27, 2010 11:00:44 AM
>Subject: Re: [Qt-interest] QThread event loop running in main thread?
>
>  Well,
>
>Maybe you guys, Nokia or whoever is responsible, should update the     
>documents. Especially the QThread-stuff. It's written there that "to     create 
>your own threads, subclass QThread and reimplement run()."
>
>Someone should write a nice good tutorial on QThread and its proper     usage. 
>It would save a lot of time from everyone. And I don't mean a     simple 
>producer-worker example.
>
>Little off-topic but hopefully nobody cares about it :)
>
>I used to do things almost like the original poster is doing and     things 
>worked. Though I don't know why his doesn't work. 
>
>
>On 27.12.2010 16:05, Thiago Macieira wrote: 
>On Monday, 27 de December de 2010 04:05:02 lbutler3 at comcast.net wrote:
>>
>>I seem to be having trouble with a QThread and signals/slots. 
>>>
>When I read the first sentence, I thought "he's using slots in a class derived 
>from QThread". So reading a bit below, we see:
>
>
>In my main window constructor I do: 
>>
>>connect(this, SIGNAL(tweakThread()), &myThread, SLOT(tweak()),
>>Qt::QueuedConnection); 
>>
Simply don't do that. DO NOT put slots in a class derived from QThread and 
you'll save yourself a lot of headaches. You can add signals, but you 
shouldn't.

Instead, create a new QObject with the signals and slots and make sure it's 
moved to or created in the other thread.

Think of QThread as "the resource managing a thread", not the work to be done. 
So "tweak" means tweaking the thread, whatever that should mean (as in "kill 
the thread", "start the thread", "set the thread's priority").


 _______________________________________________ Qt-interest mailing list 
Qt-interest at qt.nokia.com http://lists.qt.nokia.com/mailman/listinfo/qt-interest 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101227/6d97dbee/attachment.html 


More information about the Qt-interest-old mailing list