[Qt-interest] QWidget and QThread ?

Eric Clark eclark at ara.com
Fri Jun 5 21:50:11 CEST 2009


It really depends on what you are doing. Most of the time, you cannot manipulate a QWidget anywhere other than the GUI thread. This is a requirement of Qt and if you attempt to do it, Qt will crash and report an error saying something along the lines of "Cannot manipulate QWidget outside of the GUI thread," it's been a while since I have had one because I know better now. It is difficult to know whether or not the action is allowed, but I would say that you are best off never allowing manipulation to happen on a different thread. I know, for example, that you cannot hide/show/enable/disable widgets from any thread other than the GUI thread. Setting text, checking boxes, creating widgets!!! Is not allowed outside of the GUI thread. If you make an attempt at doing it, you will know that you have done something wrong because Qt will tell you.

I think this limitation is common among GUI frameworks, including MFC. I am not a platform developer like those that work at Trolltech, so I do not know the software or hardware reasons for this limitation, you would have to ask someone on Trolltech's team to know the details.

Eric

-----Original Message-----
From: Patric [mailto:userqt at gmail.com] 
Sent: Friday, June 05, 2009 1:44 PM
To: Eric Clark; qt-interest at trolltech.com
Subject: Re: [Qt-interest] QWidget and QThread ?

Hm, I see. But Eric, why is that needed ? Why the data should be manipulated 
only from the main thread ?

Patric

----- Original Message ----- 
From: "Eric Clark" <eclark at ara.com>
To: <qt-interest at trolltech.com>
Sent: Friday, June 05, 2009 6:21 PM
Subject: Re: [Qt-interest] QWidget and QThread ?


> Patric,
>
> We do some similar stuff in our application. This can be pretty tricky 
> though and you need to be very careful with the functions that manipulate 
> the data. Basically, when a function needs to manipulate the QWidget's 
> data, it needs to be done from the GUI thread. Here is our approach:
>
> void Foo()
> {
>   If this is not the main thread
>   {
>     Push the call to Foo onto the GUI thread
>     Return;
>   }
>
>   // Operate on the data of the widget
> }
>
> Basically, if you have a function, Foo, that operates on the widget, when 
> the function is called, check to see if it was called from the main GUI 
> thread. If it was not, then you need to post an event to the GUI thread to 
> call this function. How you post the event is up to you. We have a special 
> Custom event to handle these calls and we also implemented a macro called 
> ThreadCheck that checks to see if the call was made on the GUI thread, and 
> if it wasn't, it posts the event to the GUI thread, and returns out of the 
> function.
>
> This approach can be somewhat tricky, and sometimes you may need to wait 
> for the event to get processed before returning from the function. If you 
> need to wait, use some kind of static or global variable that gets set 
> when the function is done running and sit in a while loop until the 
> variable says it has been called. Inside the while loop you can sleep the 
> thread periodically.
>
> Eric
>
> -----Original Message-----
> From: qt-interest-bounces at trolltech.com 
> [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Patric
> Sent: Friday, June 05, 2009 5:11 AM
> To: Sean Harmer; qt-interest at trolltech.com
> Subject: Re: [Qt-interest] QWidget and QThread ?
>
> Well, I have an application. With tabs. Every tab have some fields and 
> every
> tab is different widget, created with QT Designer. I use the multiple
> inheritance approach here.
>
> The GUI is generated from the main thread. But I want the data in each tab
> (i.e. the fields in the tabs) to be managed by different threads, so the 
> GUI
> not freeze if there is a problem or a slower operation.
> And I'm wondering how to design this situation. It's all about.
>
> I didn't know I can't multiple inherite QObject.
>
> Regards,
> Patric
>
>
> ----- Original Message ----- 
> From: "Sean Harmer" <sean.harmer at maps-technology.com>
> To: <qt-interest at trolltech.com>
> Sent: Friday, June 05, 2009 1:03 PM
> Subject: Re: [Qt-interest] QWidget and QThread ?
>
>
>> Hi,
>>
>> On Friday 05 Jun 2009 10:56:35 Patric wrote:
>>> Hi all,
>>> I have a form, which i generate in a widget using the multiple
>>> inheritance
>>> approach. I need a thread that operates on the data of this widget. I'm
>>> wondering, is it a good idea to make my widget thread also (i.e. 
>>> multiple
>>> inheritance from QWidget and QThread), so it'll be more convinient to
>>> operate on the data. What do you think about it ?
>> In general no. You can only inherit from QObject once and both QWidget 
>> and
>> QThread are derived from QObject.
>>
>> Also all GUI operations must be performed in the main thread (the one in
>> which
>> QApplication lives).
>>
>> I would go even further and suggest that you redesign your application to
>> separate out the data and processing side from the GUI side. It will make
>> things cleaner in the long run.
>>
>> What is it you are trying to achieve? We may be able to give you some
>> nudges
>> in suitable directions?
>>
>> Cheers,
>>
>> Sean
>>
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>
>>
>> __________ Information from ESET NOD32 Antivirus, version of virus
>> signature database 4098 (20090522) __________
>>
>> The message was checked by ESET NOD32 Antivirus.
>>
>> http://www.eset.com
>>
>>
>>
>
>
> __________ Information from ESET NOD32 Antivirus, version of virus 
> signature database 4098 (20090522) __________
>
> The message was checked by ESET NOD32 Antivirus.
>
> http://www.eset.com
>
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
> __________ Information from ESET NOD32 Antivirus, version of virus 
> signature database 4098 (20090522) __________
>
> The message was checked by ESET NOD32 Antivirus.
>
> http://www.eset.com
>
>
> 


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4098 (20090522) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com







More information about the Qt-interest-old mailing list