You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Monday, February 6th 2012, 11:18pm

Terminate or kill parent process

Hi! I'm creating a simple app in QT 4.7. I have a dialog with a pushbutton and a lcdnumber to display the result from the operation done when i click in the button. This operation can be very expensive and take many time, so i used QApplication::processevents() to avoid that the application become blocked during the process.

But if an user press the pushbutton any time more after to push it before, QApplication::processevents() stop the first execution and run a new thread. Finally, when the execution finished, the second result appears in the lcdnumber and in a few milisec appears the first one (from the first execution). I want only the second result appears in the lcdnumber and if the pushbutton is clicked any time before, the first process will terminate inmediatly. I write some code that works well with a global var, but I think maybe is possibly to do it in a smart way (with eventfilter or reimplement the function event), but i'm not sure how and the examples i don't give me a solution. Plase anybody knows how it would be done without global vars? If i do an event filter function is necesary to installeventfilter? Thanks a lot and regards

//global var stop
bool stop=false;

void conditionDialog::on_aplicarButton_clicked()
{
stop=true;

for (int i=0; i<(int)quini.size(); i++) {
ui->progressBar_calculo->setValue((i+1)*100/quini.size());
if (QApplication::hasPendingEvents()) {
QApplication::processEvents();
if (!stop) return;
}
//do it some heavy work
}
stop=false;

Junior

Professional

  • "Junior" is male

Posts: 1,613

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

2

Tuesday, February 7th 2012, 2:31pm

I don't see a thread being used here.
If you don't want the user to be able to push the button a second time, then disable it while the operation is being conducted.
Then once the operation is complete then re-enable the push button.

pseudo code:
button clicked
- disable button ( btn.setEnabled( false ); )
- do operation // do processevents if not thread //
- enable button ( btn.setEnabled( true ); )

3

Tuesday, February 7th 2012, 3:40pm

Sorry, maybe I'm wrong and is not a new thread. The execution of the first call is paused and start a second when the user push the button anytime, and when the second execution finishes, the first continues at the point after the processevent() call. I don't want to disable/enable the button during this process cause it can take many time. Another idea? Thanks a lot

4

Tuesday, February 7th 2012, 5:47pm

you should push your expensive operation into a thread. If user presses the button again and you want to disregard the first push, just kill the thread and make a new one for the latest button push. Also, you should remove processEvents from your expensive method when you put it into a thread.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.