Call a function every x seconds until button pressed
Hey there guys, maybe it´s a simple task but it´s driving me a little bit crazy, especially QTime.
I´m learning minute by minute but sometimes I just need to ask something as I really need help.
What I would like to do is:
1- Press START button and every ´x´ seconds the MAIN FUNCTION is called, exactly like I would do in a for cycle and it must stop when I click STOP.
2- Press STOP button and I exit the loop;
Right now I have this:
Code:
void MainWindow::on_pushButton_START_clicked()
{
... here is the button that I want to use to START the loop of the main function. Every ´x´ seconds MAIN FUNCTION loops.
}
void MainWindow::on_pushButton_STOP_clicked()
{
... here is the button that I want to use to STOP the loop of the main function...
}
void MainWindow::on_pushButton_MAIN_FUNCTION_clicked()
{
... here is the function I want to loop...
}
Re: Call a function every x seconds until button pressed
QTimer
For 1. call QTimer::start()
For 2. call QTimer::stop()
connect the slot or signal you want to call periodically to QTimer::timeout() signal.
Re: Call a function every x seconds until button pressed
Thank you so much! The start function is working!
But how do I use the timer->stop() inside the STOP function in order to stop the timer that was created inside the START function ?
Code:
void MainWindow::on_pushButton_START_clicked()
{
connect(timer, SIGNAL(timeout()), this, SLOT(on_pushButton_MAIN_FUNCTION_clicked()));
timer->start(1000);
}
void MainWindow::on_pushButton_STOP_clicked()
{
...
}
void MainWindow::on_pushButton_MAIN_FUNCTION_clicked()
{
... here is the function I want to loop...
}
Re: Call a function every x seconds until button pressed
Keep the "timer" as member of MainWindow class.