[Qt-interest] Queued/DirectConnections for signals across threads

Thiago Macieira thiago at kde.org
Mon Dec 14 08:43:08 CET 2009


Em Segunda-feira 14. Dezembro 2009, às 06.27.49, BRM escreveu:
> class ObjectA : public QThread()
> {
> ..
> ObjectA();
> ~ObjectA();
> void run();
> protected slots:
> void readFile();
> ...
> public slots:
> void pauseRead(bool _pause);
> ...
> protected:
> bool pauseReading;
> };
> 
> 
> void ObjectA::run()
>     {
>     QTimer::singleShot(10,this,SLOT(readFile()));
>     exec();
>     }
> void ObjectA::readFile()
>     {
>     if (pauseReading == false)
>         {
>         // do your normal read stuff here
>         }
>     ...
>     QTimer::singleShot(1,this,SLOT(readFile()));
>     }
> void ObjectA::pauseRead(bool _pause)
>     {
>     pauseReading = _pause;
>     }
> 
> That should effectively do it; though you may need another object layer in
>  the thread. I usually do that - having a QThread derived class that
>  allocates a class that has all the work the thread is suppose to do, and
>  then the QThread derived class is just a signal relay class that ensures
>  the thread boundary is properly crossed.

The usual warning about using QThread as the target for signal connections: 
the slot is run in the main thread.

The code above does use the auxiliary thread's event loop to wait for 10 
milliseconds. Then it posts and event back to the main thread so that it (the 
main thread) can call readFile().

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091214/128bb5e3/attachment.bin 


More information about the Qt-interest-old mailing list