QT PushButton freeze problem
-
Hello
I have a small problem with the Push buttons on the QT. If I have a for() loop or do while logic under the push buttons, the cursor is freezing while the logic is not completely executed.
MainWinfdow.h
@private slots:
void on_pushButton_clicked ();
void on_Blower_ON_Button_pressed();
int on_Blower_OFF_Button_clicked();@With the for() loop cursor is available after it executes all logic inside the loop.
@void MainWindow::on_pushButton_2_clicked()
{
for (int i=0; i<7;i++)
{.....
}
}@But when I have a do while, it freezes completely making it infinite loop
@void MainWindow::on_Blower_ON_Button_pressed()
{do { ..... }while (on_Blower_OFF_Button_clicked());
}@
Do I need to initialize the buttons in other way, or is the a specific functions that I need to call to make the buttons non blocking elements.
-
Hi,
It's your while loop that freezes everything. Do you really need that ? What do you do in that loop ?
Also note that slots don't have return values.
-
Yes, do while loop freezes the cursor and program itself. I cannot click any other push button. My idea in this is that I have an on button (with do while loop inside ) and off button to stop doing what it was doing.
Do while loop is required to make a motor running, because I need to send the same hexadecimal value every 40ms.Is it better instead of do while achieve the same result with the interrupts or make a parallel routine?
-
Way simpler: use either a QTimer or the timerEvent from your widget.
QTimer:
configure the timer to use a 40ms interval
connect the QTimer timeout signal to a custom slot sending the value
connect your start button to the QTimer start slot
connect your stop button to the QTimer stop slot
No loop needed