Results 1 to 9 of 9

Thread: need advice on gui thread

  1. #1
    Join Date
    Nov 2012
    Posts
    21
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default need advice on gui thread

    Hi all,

    I am new to Qt/QThread.

    What is the best way to handle Worker thread in GUI thread. Qt has GUI/Worker thread concepts?

    I have to show progress on GUI while copying folder. Can somebody suggest in brief. It should allow to quit the program whenever I want to quit the application from context menu option.

    Thanks

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: need advice on gui thread

    There is no need for threads to display progress while you copy files. Have a look at QProgressBar: connect a signal emitted as files are copied to the setValue() slot of QProgressBar.

    You can quit the program at any time by connecting a signal to qApp's quit() slot (see QCoreApplication::quit()).

  3. The following user says thank you to ChrisW67 for this useful post:

    dpn (15th September 2013)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: need advice on gui thread

    The program might need a thread to do the copying, e.g. if QFile::copy() should be used because that one is blocking.

    In any case progress reporting works the same way: the code that copies emits progress signals, which are connected to a progress bar or progress dialog.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    dpn (15th September 2013)

  6. #4
    Join Date
    Nov 2012
    Posts
    21
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default which is the correct implementation for worker threas

    Hi,

    I have goggled for qt worker threads. I am new to Qt. I found three kinds of implementations.

    1. Inherit worker class from QThread and override Run()
    2. Create QThread object as member variable of worker class
    3. make it as a thread by using movetothread.

    In which scenario, I have to use 1st approach. If possible, plz explain about other approaches.

    Thanks in advance.

  7. #5
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: which is the correct implementation for worker threas

    Qt Code:
    1. class Worker : public QObject {
    2. Q_OBJECT
    3.  
    4. public:
    5. worker()//constructor
    6. public slots:
    7. void process(); // do your work here
    8.  
    9. signals:
    10. void finished(); // emit when done
    11. };
    To copy to clipboard, switch view to plain text mode 

    and connect it in a way similar to this:

    Qt Code:
    1. QThread* thread = new QThread;
    2. Worker* worker = new Worker();
    3. worker->moveToThread(thread);
    4. connect(thread, SIGNAL(started()), worker, SLOT(process()));
    5. connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
    6. connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
    7. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    8. thread->start();
    To copy to clipboard, switch view to plain text mode 

    That would be a good way of working with threads in Qt and its quit interesting and flexible and easy.
    hope that helps Best Reagrds.

  8. The following user says thank you to toufic.dbouk for this useful post:

    dpn (15th September 2013)

  9. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: which is the correct implementation for worker threas

    Quote Originally Posted by dpn View Post
    I have goggled for qt worker threads. I am new to Qt. I found three kinds of implementations.

    1. Inherit worker class from QThread and override Run()
    2. Create QThread object as member variable of worker class
    3. make it as a thread by using movetothread.
    You have two options here: 1 and 3. 2 alone does not make the worker code execute in the second thread.

    Quote Originally Posted by dpn View Post
    In which scenario, I have to use 1st approach. If possible, plz explain about other approaches.
    The 1st approach is useful if the parallized operation does not need an event loop, e.g. some form of blocking loop.
    The other approach is useful if you need an event loop.

    Cheers,
    _

  10. The following user says thank you to anda_skoa for this useful post:

    dpn (16th September 2013)

  11. #7
    Join Date
    Nov 2012
    Posts
    21
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: which is the correct implementation for worker threas

    Should application delete thread and worker objects?

  12. #8
    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: which is the correct implementation for worker threas

    Quote Originally Posted by dpn View Post
    Should application delete thread and worker objects?
    The application should always clean up the mess it has done.
    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.


  13. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: which is the correct implementation for worker threas

    The real answer (to the original thread question), as most of the time is: "it depends".
    On many things and considerations.
    So a real "right way of doing it" is not something absolute.
    Usually however, if you design your code well and in OOP way, what ever it is you want your worker thread to do should be encapsulated in a way which is not dependent (but capable) of running in a thread.
    This usually will translate to NOT needing to subclass a QThread.
    This issue got to be somewhat of a religious issue over the past years.
    Different people just feel that one way is more right than the other.
    The test is in the real word: which implementation fits your design the best way on all fronts:
    readability, re-usability, maintenance (such the the open-close principal) etc.
    Here is a interesting post by an (ex?) troll, where he answers another blog post (be sure to read it, its linked in the blog text!).
    Once you have read it, make the best decision for your case.
    And don't let anyone else tell you if its wrong or right, unless they can prove their claim with valid points to your specific case.
    http://woboq.com/blog/qthread-you-we...-so-wrong.html
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 1
    Last Post: 25th October 2012, 15:10
  2. Replies: 1
    Last Post: 28th March 2012, 18:58
  3. Replies: 9
    Last Post: 28th November 2009, 20:31
  4. Thread design advice needed
    By jonks in forum Qt Programming
    Replies: 3
    Last Post: 22nd October 2009, 21:32
  5. Advice needed regarding GUI and external thread cooperation
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 17th June 2009, 00:10

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.