[Qt-interest] QWidget and QThread ?

Eric Clark eclark at ara.com
Fri Jun 5 17:21:05 CEST 2009


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




More information about the Qt-interest-old mailing list