Results 1 to 12 of 12

Thread: Make ui stay responsive when executin thread

  1. #1
    Join Date
    Jan 2013
    Posts
    43
    Thanks
    27
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Make ui stay responsive when executin thread

    I subclassed QThread and it's run method like this:

    mythread.h
    Qt Code:
    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QString>
    6.  
    7. class myThread : public QThread
    8. {
    9. Q_OBJECT
    10. public:
    11. myThread();
    12. void run();
    13.  
    14. private:
    15. //...
    16. };
    17.  
    18. #endif // MYTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    mythread.cpp
    Qt Code:
    1. #include "mythread.h"
    2. #include <iostream>
    3.  
    4. myThread::myThread()
    5. {
    6. //...
    7. }
    8.  
    9. void myThread::run()
    10. {
    11. for(int i=0; i<10; i++)
    12. {
    13. QThread::sleep(1);
    14. std::cout << i << std::endl;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    How and when can i call this thread in order que keep the application responsive?

    Sorry if it makes no sense ... my first thread experience

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Make ui stay responsive when executin thread

    How and when can i call this thread in order que keep the application responsive?
    It doesn't matter, whenever you want...
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jan 2013
    Posts
    43
    Thanks
    27
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Make ui stay responsive when executin thread

    How is that?
    i'm calling it from QMainWindow and i have to wait 10 seconds before i hava access to the menu again!

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

    Default Re: Make ui stay responsive when executin thread

    Quote Originally Posted by aguleo View Post
    i'm calling it
    "It", being what exactly?
    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.


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

    aguleo (26th January 2013)

  6. #5
    Join Date
    Jan 2013
    Posts
    43
    Thanks
    27
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Make ui stay responsive when executin thread

    I attach "it"
    I guess i can not call it from that member ... isn't it?
    Attached Files Attached Files
    • File Type: zip w.zip (4.8 KB, 5 views)

  7. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Make ui stay responsive when executin thread

    How is that?
    i'm calling it from QMainWindow and i have to wait 10 seconds before i hava access to the menu again!
    No it won't wait, (That is whats Thread's do)

    To run the thread you should be calling QThread::start() (not QThread::run()), this will run the thread in it own (new thread) context

    If you call QThread::run(), it will execute in the calling thread context.
    Last edited by Santosh Reddy; 26th January 2013 at 19:45.

  8. The following user says thank you to Santosh Reddy for this useful post:

    aguleo (26th January 2013)

  9. #7
    Join Date
    Jan 2013
    Posts
    43
    Thanks
    27
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Make ui stay responsive when executin thread

    Ok ... made some progress.
    I was creating the thread inside a member function ... this way it ended unexpectedly ...
    But now i have a question. What happens when i call the same option again while it is still running?
    I ask because apparently nothing does!
    Attached Files Attached Files
    • File Type: zip w.zip (4.8 KB, 5 views)

  10. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Make ui stay responsive when executin thread

    But now i have a question. What happens when i call the same option again while it is still running?
    If the thread is running, then nothing will happen.
    If the thread is not running, the thread will start running, provided all waiting conditions are cleared.

  11. The following user says thank you to Santosh Reddy for this useful post:

    aguleo (26th January 2013)

  12. #9
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Make ui stay responsive when executin thread

    if you want to run another thread instance, then create an array of threads and run them as you require. don't forget to capture their ending event so you know which are running.
    if you need to synchronize concurrent access to shared item, conside QSharedMemory or QSempahore.

  13. The following user says thank you to lightydo for this useful post:

    aguleo (26th January 2013)

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

    Default Re: Make ui stay responsive when executin thread

    Maybe you should first tell us what you want the thread to do?
    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.


  15. #11
    Join Date
    Jan 2013
    Posts
    43
    Thanks
    27
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Make ui stay responsive when executin thread

    There was nothing specific on my mind. I read something about saving large files to disk in the "C++ GUI programming with Qt4" and got curiouse.
    Maybe you should first tell us what you want the thread to do?
    You mean that the approach will depend on what i want to do ... is it so?

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

    Default Re: Make ui stay responsive when executin thread

    Yes, for many usecases using a thread is either not possible or simply bad idea.

    Here, read this: [wiki]Keeping the GUI Responsive[/wiki]
    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.


  17. The following user says thank you to wysota for this useful post:

    aguleo (28th January 2013)

Similar Threads

  1. How to make a window stay on top of all others?
    By Momergil in forum Qt Programming
    Replies: 9
    Last Post: 16th July 2012, 20:25
  2. Make frameless tool window stay on top of the application
    By ainne810329 in forum Qt Programming
    Replies: 3
    Last Post: 16th January 2012, 11:37
  3. Replies: 0
    Last Post: 15th December 2011, 10:26
  4. Keeping your application responsive
    By chezifresh in forum Qt Programming
    Replies: 6
    Last Post: 30th March 2011, 01:45
  5. How to keep the GUI responsive?
    By drjones in forum Qt Programming
    Replies: 8
    Last Post: 16th July 2008, 14:33

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.