Waiting x seconds before continuing
-
I'm trying to change a label in an onButtonClicked function, then change it back after x seconds. I've tried using sleep() but since it executes before the initial label change so I tried using a QTimer in the following way:
QTimer *timer = new QTimer(this); timer->start(4000); if( timer->remainingTime() <=0 ){ //label change goes here delete timer; }
but it doesn't actually do anything, any ideas?
-
You should take a look at the documentation to of QTimer - there is an example which does exactly what you want to do: https://doc.qt.io/qt-5/qtimer.html#details
-
You should take a look at the documentation to of QTimer - there is an example which does exactly what you want to do: https://doc.qt.io/qt-5/qtimer.html#details
@Christian-Ehrlicher sorry but which one is the example?
-
@Christian-Ehrlicher sorry but which one is the example?
@John-Green
Hi
I assume he meant the singleslot example.
Its perfect fit anyway.QTimer::singleShot(1000, this, [this](){ this->ui->label->setText("BACK"); });
this sets label to "BACK" after 1 sec.
we are using the static function version so we avoid to
new QTimer and also delete it again.
singleShot is perfect for such delay.Also, please dont use sleep in GUI thread. it will just hang whole app and never
really do what you want. -
@John-Green
Hi
I assume he meant the singleslot example.
Its perfect fit anyway.QTimer::singleShot(1000, this, [this](){ this->ui->label->setText("BACK"); });
this sets label to "BACK" after 1 sec.
we are using the static function version so we avoid to
new QTimer and also delete it again.
singleShot is perfect for such delay.Also, please dont use sleep in GUI thread. it will just hang whole app and never
really do what you want.@mrjj Hi thanks for the help however this piece of code throws no matching function call error, any ideas why?
-
@mrjj Hi thanks for the help however this piece of code throws no matching function call error, any ideas why?
@John-Green
Did you include
#include <QTimer>
?
please post the actual error :) -
@John-Green
Did you include
#include <QTimer>
?
please post the actual error :)@mrjj yes I have it included
error: no matching function for call to 'QTimer::singleShot(int, MainWindow*, MainWindow::on_Button_clicked()::<lambda()>)'
});
^ -
@mrjj yes I have it included
error: no matching function for call to 'QTimer::singleShot(int, MainWindow*, MainWindow::on_Button_clicked()::<lambda()>)'
});
^@John-Green
Hi
Can you show the actual code ?
Im using a lambda, but it seems you used normal slot ?
(which is also possible)MainWindow::on_Button_clicked()::<lambda()>
looks like you tell it the lambda is in mainwindow so syntax might be wrong :) -
@John-Green
Hi
Can you show the actual code ?
Im using a lambda, but it seems you used normal slot ?
(which is also possible)MainWindow::on_Button_clicked()::<lambda()>
looks like you tell it the lambda is in mainwindow so syntax might be wrong :)@mrjj I just used the code you provided and didn't change anything:
QTimer::singleShot(1000, this, [this](){ this->ui->label->setText("BACK"); });
-
@mrjj I just used the code you provided and didn't change anything:
QTimer::singleShot(1000, this, [this](){ this->ui->label->setText("BACK"); });
@John-Green
That is odd.
Are you using an older Qt / compiler ?
Lambdas came later.Anyway, you can also use a normal slot if you want
QTimer::singleShot(1000, this, &MainWindow::on_pushButton_5_released);