[Qt-interest] QWidget and QThread ?

Eric Clark eclark at ara.com
Fri Jun 5 22:37:08 CEST 2009


Ok... like I said... not all functions cause errors. However, many do, and if you want to do this kind of thing, just pay close attention to the docs when doing it; make sure the function is thread-safe. Be careful in any slots connected to the textChanged signal or any signal you may be connected to. Use mutexes when necessary. Just take note, that if you see the error you just saw, the function cannot be called outside the GUI thread and you need to do something to cause it to run on the GUI thread like the example I gave. Good luck!

Eric

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

Yes... runtime error...

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


> Did you try to hide it?
>
> -----Original Message-----
> From: Patric [mailto:userqt at gmail.com]
> Sent: Friday, June 05, 2009 3:24 PM
> To: Eric Clark; qt-interest at trolltech.com
> Subject: Re: [Qt-interest] QWidget and QThread ?
>
> It's QLineEdit. But I don't use signals&slots, I'm just calling the method
> of the object from different thread. It's QLineEdit in my custom widget.
>
> Patric
>
> ----- Original Message ----- 
> From: "Eric Clark" <eclark at ara.com>
> To: <qt-interest at trolltech.com>
> Sent: Friday, June 05, 2009 11:12 PM
> Subject: Re: [Qt-interest] QWidget and QThread ?
>
>
>> Are you sure that it is coming from a different thread? Try hiding the
>> widget. What type of widget is it? Is it your own special widget, or is 
>> it
>> a QLineEdit or something?
>>
>> -----Original Message-----
>> From: Patric [mailto:userqt at gmail.com]
>> Sent: Friday, June 05, 2009 3:01 PM
>> To: Eric Clark; qt-interest at trolltech.com
>> Subject: Re: [Qt-interest] QWidget and QThread ?
>>
>> Hm, it's strange. I just did it 5 minutes ago.
>> I set the text of a line in my widget from a different thread trought the
>> *ui variable.
>> There was so error message and the text changed...
>>
>> Patric
>> ----- Original Message ----- 
>> From: "Eric Clark" <eclark at ara.com>
>> To: <qt-interest at trolltech.com>
>> Sent: Friday, June 05, 2009 10:50 PM
>> Subject: Re: [Qt-interest] QWidget and QThread ?
>>
>>
>>> 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
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> 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
>>
>>
>> __________ 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
>
>
> __________ 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