Results 1 to 5 of 5

Thread: Worker thread

  1. #1
    Join Date
    Jun 2009
    Posts
    66
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Worker thread

    Hi,

    I've got a 3rd party blocking library that i'm trying to implement into my QT GUI application. Using a QThread, signals and slots, but i'm struggling to work out the best way to do things.

    The library contains a number of different API calls which I will want to call at various points, and then return the reult to the main GUI thread.

    So far I've created a class as follows

    Qt Code:
    1. class LibraryManagerThread: public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. LibraryManagerThread();
    6. ~LibraryManagerThread();
    7.  
    8. protected:
    9. void run();
    10.  
    11. signals:
    12. void done(); // Signal back to main GUI thread when work is done.
    13.  
    14. private slots:
    15. /* place slots for each blocking api call here */
    16. };
    To copy to clipboard, switch view to plain text mode 

    In the class code I have the following :-

    Qt Code:
    1. LibraryManagerThread::LibraryManagerThread()
    2. {
    3. // We have to do this to make sure our thread has the
    4. // correct affinity.
    5. moveToThread(this);
    6.  
    7. start();
    8. }
    9.  
    10. void LibraryManagerThread::run()
    11. {
    12. // This starts the event loop.
    13. exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    So the event loop will process any events I pass to it. So what i'm thinking is to signal from the main event loop to this thread, and then signal back to the main event loop once the work has been done.

    However, i'm wondering how best to make this code portable, because using my method above, the LibraryManagerThread needs to know about the signals i am going to emit from the main GUI thread, hence it would then require code changes to use this class in a different application.

    any thoughts how i could get around this?

    cheers,

    David

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Worker thread

    It doesn't need to know about the signals. It just needs to expose a set of slots conforming to the functionality it contains. The rest is left to the environment of the class that needs to connect those signals properly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2009
    Posts
    66
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Worker thread

    Cool. So I make the connections to those public slots in my main GUI thread.

    cheers

  4. #4
    Join Date
    Nov 2008
    Location
    Italy
    Posts
    16
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Worker thread

    Why do you use moveToThread in the constructor? Do you use object created in another thread in your run method?

    Thanks

  5. #5
    Join Date
    Jun 2009
    Posts
    66
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Worker thread

    To be honest, i'm not sure why i have this in there, I got the code from another example, and that is what it had.

    My worker thread is coming along now and my gui is nice and smooth.

    However i think i'm going to have to impleament some sort of thread pool for api calls.

    At the moment i have one thread which queues all my calls into the blocking library, but when calls into this thread are in quick succession, this can be slow.

Similar Threads

  1. Replies: 4
    Last Post: 11th September 2009, 05:23
  2. Worker thread problem
    By hkvm in forum Qt Programming
    Replies: 4
    Last Post: 6th September 2009, 20:12
  3. Showing progress bar while worker thread works
    By olidem in forum Qt Programming
    Replies: 6
    Last Post: 27th April 2009, 20:43
  4. Accessing data from a worker thread
    By steg90 in forum Qt Programming
    Replies: 20
    Last Post: 25th May 2007, 10:20
  5. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.