how to break the loop when running the loop.
-
wrote on 2 Jan 2023, 05:22 last edited by
This is my code,
I have started the timer as initially and set the pushbutton to run on it and when pushbutton 2 is clicked the timer will stop but when the loop is running when pushbutton 2 is clicked it is not working. The button I clicked runs after the loop reaches target 100. How to fix this
MainWindow.CPP
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi->(this); Functionstart = new QTimer(this); connect(Functionstart ,SIGNAL(timeout()) , this ,SLOT(on_pushButton_clicked())); Functionstart->start(1000); } void MainWindow::on_pushButton_clicked() { for(int i=0;i<=100;i++) { if(i==100) { ui->stackedWidget->setCurrentIndex(2); } else { qDebug()<<"Loop Running"; } } } void MainWindow::on_pushButton_2_clicked() { Functionstart->stop(); }
Thanks in advance,
-
wrote on 2 Jan 2023, 06:36 last edited by ChrisW67 1 Feb 2023, 06:39
The Qt event loop is necessary to dispatch the timeout() signal. The on_pushbutton_clicked() slot will not return to the Qt event loop until on_pushButton_clicked() finishes doing 101 seemingly pointless loops setting the stacked widget current page on the last.
Perhaps you can explain what you are trying to achieve.
-
wrote on 3 Jan 2023, 02:36 last edited byThis post is deleted!
1/3