PDA

View Full Version : Show progress of a time consuming operation



rainman110
9th February 2008, 21:36
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:


class ProgressDialog : public QWidget {
public:
signalProgress(); //the bar should move a bit to right
setTotalProgress(int); //maxValue of progress bar
}


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:



void doComplicatedStuff(ProgressDialog& pd){
while(calculating){
someCalculation;
pd.signalProgress();
}
}

main(argc,argv){
QApplication a(argc,argv);
ProgressDialog pd;
pd.show();
doComplicatedStuff(pd);//no dialog visible
return a.exec(); //here, the dialog pops up - swapping last lines is no solution
}


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

Cheers

jpn
9th February 2008, 21:39
Hi, you might want to take a look at QProgressDialog. :)

wysota
9th February 2008, 22:35
And QCoreApplication::processEvents.

rainman110
10th February 2008, 10:57
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).

wysota
10th February 2008, 10:59
With processEvents() you can.... process pending events thus making your application responsive.

rainman110
10th February 2008, 11:07
I think I have to do more reading about this stuff. Thank you!

rainman110
10th February 2008, 11:35
Okay... i read a bit about it. How should be now the semantic of the main function

like this?:


int main(argc,argv){
QApplication app(argc,argv);
QProgressDialog pd();
pd.setrange(0,100);

//now the calculation loop?
for(int i = 0; i < 100, ++i){
someCalculation;
pd.setValue(i);
app.processEvents();
}
return app.exec();
}


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?

jpn
10th February 2008, 12:07
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...