Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, April 2008
Creating QObject not in GUI thread


Message 1 in thread

Hi,

Is it ok to create object of class derived from QObject in not GUI thread ?
For class derived from QWidget that seems to be an error.

Thanks,
Vladimir

--
 [ signature omitted ] 

Message 2 in thread

Am Freitag, 18. April 2008 schrieb Vladimir Romanovskiy:
> Is it ok to create object of class derived from QObject in not GUI thread ?
> For class derived from QWidget that seems to be an error.

As far as I know you need qt4 for that. But then its okay, since every qthread 
has its own event-loop. So even signals and slots and events work across the 
thread-boundaries.

The only case where it doesn't work is everything connected with gui, since 
gui can only be done in the main-thread and you shouldn't access gui stuff 
from other threads as that would block the gui. So you have to communicate 
with the main-thread either by some own non-blocking mechanism or just use 
signals/events.

Arnold
-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 3 in thread

Vladimir Romanovskiy wrote:
>Hi,
>
>Is it ok to create object of class derived from QObject in not GUI
> thread ? For class derived from QWidget that seems to be an error.

Yes, you're allowed to create a QObject-inherited class that is not 
QWidget-derived in any thread.

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 4 in thread

Thiago Macieira wrote:
> Vladimir Romanovskiy wrote:
>   
>> Hi,
>>
>> Is it ok to create object of class derived from QObject in not GUI
>> thread ? For class derived from QWidget that seems to be an error.
>>     
>
> Yes, you're allowed to create a QObject-inherited class that is not 
> QWidget-derived in any thread.
>
>   
First question:
If a QObject is created not in Qt thread (does not have Qt event-loop) 
then call
QCoreApplication::sendEvent(QObject *receiver, QEvent *event) fails .

That is because the sendEvent() results in the call 
QCoreApplicationPrivate::checkReceiverThread()

void QCoreApplicationPrivate::checkReceiverThread(QObject *receiver)
{
   QThread *currentThread = QThread::currentThread();
   QThread *thr = receiver->thread();
   Q_ASSERT_X(currentThread == thr || !thr
                             ....

Does that mean that QObject must be created (or moved with 
QObject::moveToThread() ) in the Qt thread ?


Second question:
What happens if QObject is created in not Qt threat and then it is made 
the call :
void QCoreApplication::postEvent ( QObject  * receiver, QEvent * event ) ?

In event queue of which Qt thread this event will be send if there are 
multiple Qt threads ( with Qt event loops)
running simultaneously?

Thanks,
Vladimir

--
 [ signature omitted ] 

Message 5 in thread

Thiago Macieira wrote:
> Vladimir Romanovskiy wrote:
>   
>> Hi,
>>
>> Is it ok to create object of class derived from QObject in not GUI
>> thread ? For class derived from QWidget that seems to be an error.
>>     
>
> Yes, you're allowed to create a QObject-inherited class that is not 
> QWidget-derived in any thread.
>
>   
First question:
If a QObject is created not in Qt thread (does not have Qt event-loop) 
then call
QCoreApplication::sendEvent(QObject *receiver, QEvent *event) fails .

That is because the sendEvent() results in the call 
QCoreApplicationPrivate::checkReceiverThread()

void QCoreApplicationPrivate::checkReceiverThread(QObject *receiver)
{
  QThread *currentThread = QThread::currentThread();
  QThread *thr = receiver->thread();
  Q_ASSERT_X(currentThread == thr || !thr
                            ....

Does that mean that QObject must be created (or moved with 
QObject::moveToThread() ) in the Qt thread ?


Second question:
What happens if QObject is created in not Qt threat and then it is made 
the call :
void QCoreApplication::postEvent ( QObject  * receiver, QEvent * event ) ?

In event queue of which Qt thread this event will be send if there are 
multiple Qt threads ( with Qt event loops)
running simultaneously?

Thanks,
Vladimir