Results 1 to 8 of 8

Thread: Show progress of a time consuming operation

  1. #1
    Join Date
    Feb 2008
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Show progress of a time consuming operation

    Hi,

    i am writing a program which calculates the irradiation dose for cancer treatment. This can take some time, so my idea was, to show up a simple dialog with a progress-bar in it. I dont know really how to do it because when the dialog is started, the program stops as long it is opened.

    I like to implement such an interface:
    Qt Code:
    1. class ProgressDialog : public QWidget {
    2. public:
    3. signalProgress(); //the bar should move a bit to right
    4. setTotalProgress(int); //maxValue of progress bar
    5. }
    To copy to clipboard, switch view to plain text mode 

    My programm is a console program actually. I dont want to do all the calulation stuff in a window. So - how should i implement the main() function. My idea was like following, but this won't work:

    Qt Code:
    1. void doComplicatedStuff(ProgressDialog& pd){
    2. while(calculating){
    3. someCalculation;
    4. pd.signalProgress();
    5. }
    6. }
    7.  
    8. main(argc,argv){
    9. QApplication a(argc,argv);
    10. ProgressDialog pd;
    11. pd.show();
    12. doComplicatedStuff(pd);//no dialog visible
    13. return a.exec(); //here, the dialog pops up - swapping last lines is no solution
    14. }
    To copy to clipboard, switch view to plain text mode 

    So what can i do instead? I hope, you can help me.
    Thanks! Thanks! Thanks!

    Cheers

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Show progress of a time consuming operation

    Hi, you might want to take a look at QProgressDialog.
    J-P Nurmi

  3. #3
    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: Show progress of a time consuming operation


  4. #4
    Join Date
    Feb 2008
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Show progress of a time consuming operation

    Thanks for your reply! I have found QProgressDialog already but it doesnt solve my problem, that the application is unresponsible.
    What can I do with QCoreApplication::processEvents? I am really a newbie in qt programming.
    I tried to do it with seperate threads - the main thread for the dialog, an extra thread for the calculation - but there the problem is, that you can't connect signal and slots from different threads (runtime exception).

  5. #5
    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: Show progress of a time consuming operation

    With processEvents() you can.... process pending events thus making your application responsive.

  6. #6
    Join Date
    Feb 2008
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Show progress of a time consuming operation

    I think I have to do more reading about this stuff. Thank you!

  7. #7
    Join Date
    Feb 2008
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Show progress of a time consuming operation

    Okay... i read a bit about it. How should be now the semantic of the main function

    like this?:
    Qt Code:
    1. int main(argc,argv){
    2. QApplication app(argc,argv);
    3. pd.setrange(0,100);
    4.  
    5. //now the calculation loop?
    6. for(int i = 0; i < 100, ++i){
    7. someCalculation;
    8. pd.setValue(i);
    9. app.processEvents();
    10. }
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Is this right? I dont know really, where to place the app.exec - why i need it.
    Is this the right order, or should the app.exec() be placed over the calculation loop?

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Show progress of a time consuming operation

    You don't have to enter to the event loop of the application at all if showing a progress bar is all what your application does...
    J-P Nurmi

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.