How to prevent GUI from hanging while allowing 2nd operation to be performed alongwith 1st operation!
-
Here I am explaining my problem statement in detail and the efforts I have put so far
Problem Statement : During printing if 'Stop Printing' pushbutton is pressed, the printing should stop at that moment!
*My Work * -
-
StartPrinitng_Pressed :
void MainWindow :: on_StartPrinitng_Pressed() { while(studentList.next()) { studentList("SELECT Name, address FROM class WHERE Roll No = some variable") Name=studentList.value(0).toString(); address=studentList.value(1).toString(); QTimer:: singleShot(1000,this,SLOT(StopNow())); //calling stopNow function if(StopPrintingNow==0) { //** I am printing the fetched data (in a string) by setting GPIO pins HIGH **// } else if(StopPrintingNow==1) { //** I have reset all serials ports; Break; **// } } }
-
StopPrinting_Pressed :
void MainWindow::on_StopPrinting_Pressed() {StopPrintingNow=1;}
-
StopNow Function Declaration :
void MainWindow::StopNow() { if(StopPrintingNow==1) { //** I have reset all serials ports; Break; **// } else if(StopPrintingNow==0) { QTimer::singleShot(1000,this,SLOT(on_startPrinting_pressed())); } }
Flow of program execution : As and when "StartPrinting" pushbutton is pressed, the query shown in my question executes which fetches data from database and perform simultaneous printing.
Problem Faced -
1.GUI is getting hanged while printing, hence StopPrinting button doesn't respond.
2. Qtimer is not calling "StopNow function " while printing (though I have called it at correct position) -
-
Hi, I think the SQL query is not your problem, it should be fast.
Your problem is that you transmit the whole serial data during the slot. In this time the event loop is blocked and therefore the stop button is not working.
Two possibilities:
- Only send some characters at a time, and then send the next chars in a timer slot, until all chars are sent.
- Move the whole serial sending into a second thread
Regards
-
And please don't cross post!
This is exactly the same as https://forum.qt.io/topic/92674/how-to-break-while-loop-using-some-conditional-check/6
Closing this one.