[Interest] How to start object in the thread?

Roland Winklmeier roland.m.winklmeier at gmail.com
Sun Jun 8 11:43:30 CEST 2014


On 06/08/2014 12:10 PM, igor.mironchik at gmail.com wrote:
> 
> QCoreApplication app( argc, argv );
> 
> Object o;
> QThread t;
> o.moveToThread( &t );
> t.start();
> 
> QTimer::singleShot( 0, &o, SLOT( start() ) );
> 

Hi Igor,

why are you using a QTimer? As far as I know, you could invoke the slot
directly.

QMetaObject::invokeMethod(&o, "start", Qt::QueuedConnection);

This will place an event in the threads event loop. This works in
several places in my project. Maybe there is a better solution.
In principle you can also omit Qt::QueuedConnection.

QMetaObject::invokeMethod(&o, "start");

Default is Qt::AutoConnection, which detects o is running in a different
thread. But I usually pass QueuedConnection explicitly.

Roland






More information about the Interest mailing list