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 * -

    1. StartPrinitng_Pressed :

       void MainWindow :: on_StartPrinitng_Pressed()
        {  studentList("SELECT Name, address FROM class  WHERE Roll No = some    variable")
        QTimer:: singleShot(1000,this,SLOT(StopNow()));   //calling stopNow function
        {  //**   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;   **//  }
    2. StopPrinting_Pressed :

      void MainWindow::on_StopPrinting_Pressed()
    3. StopNow Function Declaration :

       void MainWindow::StopNow() 
         {   //**  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)

  • Lifetime Qt Champion


    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:

    1. Only send some characters at a time, and then send the next chars in a timer slot, until all chars are sent.
    2. Move the whole serial sending into a second thread


  • Lifetime Qt Champion


    And please don't cross post!

    This is exactly the same as

    Closing this one.

Log in to reply