Results 1 to 15 of 15

Thread: QThread and QEventLoop - Idle Processing

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2007
    Posts
    14
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default

    After reading the documentation, I'm still not quite clear on how to use a QEventLoop to do idle processing in a QThread. Basically I want to have a thread that connects a device. Once a device is connected I want to use idle processing to poll the device. The user can terminate the thread using the quit function (which disconnects the device if necessary). As far as I understand, this is how you make your thread enter an event loop when it is started.

    Qt Code:
    1. void MyThread::run()
    2. {
    3. exec();
    4. }
    To copy to clipboard, switch view to plain text mode 

    So my question basically is: where do I put the code that does the polling of the device?

    To make your application perform idle processing (i.e. executing a
    special function whenever there are no pending events), use a
    QTimer with 0 timeout. More sophisticated idle processing schemes
    can be achieved using processEvents().
    Can someone post an example for this? What are the " sophisticated idle processing schemes"?

    Here is a rough overview of how my run function would look without an event loop:

    Qt Code:
    1. void MyThread::run()
    2. {
    3. connect_device();
    4.  
    5. done = false;
    6.  
    7. while(!done)
    8. {
    9. poll_device();
    10. }
    11.  
    12. disconnect_device();
    13. }
    14.  
    15. void MyThread::quit()
    16. {
    17. done = true;
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 26th February 2008 at 00:57. Reason: Merged two posts

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.