QT delay not working
-
hi
this my code .i want change text and color of the button after delay.
only 1st delay working but its not taking 2nd delay
void MainWindow::on_pushButtonSystemswitchon_clicked()
{ui->pushButtonSystemswitchon->setStyleSheet("font-size: 20px;" "background: red;"); ui->pushButtonSystemswitchon->setText("ON"); ui->pushButtonFeeder->setEnabled(false); ui->pushButtonEejecter->setEnabled(false);
QThread::msleep(1000);
ui->pushButtonFeeder->setStyleSheet("font-size: 20px;" "background: green;"); ui->pushButtonFeeder->setText("FEEDER ON"); FEEDER_button_flag=0; FEEDER_check_flag=false;
QThread::msleep(1000);
ui->pushButtonEejecter->setStyleSheet("font-size: 20px;"
"background: blue;");
ui->pushButtonEejecter->setText("EJECTER ON");
EJECTER_button_flag=0;
EJECTER_check_flag=false;power_button_flag=0; power_check_flag=false;
}
-
In a world with multi thread.. Qthread could be only a single thread... anyway, this is not my point... Why you do not use QAnimation?? have a look here https://doc.qt.io/qt-5/animation-overview.html
-
hi
this my code .i want change text and color of the button after delay.
only 1st delay working but its not taking 2nd delay
void MainWindow::on_pushButtonSystemswitchon_clicked()
{ui->pushButtonSystemswitchon->setStyleSheet("font-size: 20px;" "background: red;"); ui->pushButtonSystemswitchon->setText("ON"); ui->pushButtonFeeder->setEnabled(false); ui->pushButtonEejecter->setEnabled(false);
QThread::msleep(1000);
ui->pushButtonFeeder->setStyleSheet("font-size: 20px;" "background: green;"); ui->pushButtonFeeder->setText("FEEDER ON"); FEEDER_button_flag=0; FEEDER_check_flag=false;
QThread::msleep(1000);
ui->pushButtonEejecter->setStyleSheet("font-size: 20px;"
"background: blue;");
ui->pushButtonEejecter->setText("EJECTER ON");
EJECTER_button_flag=0;
EJECTER_check_flag=false;power_button_flag=0; power_check_flag=false;
}
For the love of code. Do not use QThread::sleep if you do not know how to use it and what it actually does!
-
Hi
It is not good to use QThread::msleep();
in a GUI program as you completely freeze all drawing and input and
it not suitable for any kind of delay in any sort of animation.In this case, a single shot timer is good for such a delay.
QTimer::singleShot(1000, [ this]{
qDebug("Hello from lambda");
ui->pushButtonFeeder->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButtonFeeder->setText("FEEDER ON");
}); -
hi
this my code .i want change text and color of the button after delay.
only 1st delay working but its not taking 2nd delay
void MainWindow::on_pushButtonSystemswitchon_clicked()
{ui->pushButtonSystemswitchon->setStyleSheet("font-size: 20px;" "background: red;"); ui->pushButtonSystemswitchon->setText("ON"); ui->pushButtonFeeder->setEnabled(false); ui->pushButtonEejecter->setEnabled(false);
QThread::msleep(1000);
ui->pushButtonFeeder->setStyleSheet("font-size: 20px;" "background: green;"); ui->pushButtonFeeder->setText("FEEDER ON"); FEEDER_button_flag=0; FEEDER_check_flag=false;
QThread::msleep(1000);
ui->pushButtonEejecter->setStyleSheet("font-size: 20px;"
"background: blue;");
ui->pushButtonEejecter->setText("EJECTER ON");
EJECTER_button_flag=0;
EJECTER_check_flag=false;power_button_flag=0; power_check_flag=false;
}
@swansorter This is not how you should do this! Don't block your main thread with sleep()!
Use https://doc.qt.io/qt-5/qtimer.html#singleShot instead -
Hi
It is not good to use QThread::msleep();
in a GUI program as you completely freeze all drawing and input and
it not suitable for any kind of delay in any sort of animation.In this case, a single shot timer is good for such a delay.
QTimer::singleShot(1000, [ this]{
qDebug("Hello from lambda");
ui->pushButtonFeeder->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButtonFeeder->setText("FEEDER ON");
});@mrjj this working for me..but
1.if i give same delay value it exicute at the same time
QTimer::singleShot(1000, [ this]{
qDebug("Hello from lambda");
ui->pushButton1->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton1->setText("FEEDER ON");
});
QTimer::singleShot(1000, [ this]{
qDebug("Hello from lambda");
ui->pushButton2->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton2->setText("FEEDER ON");
});
2.if give different time value the it works
QTimer::singleShot(500, [ this]{
qDebug("Hello from lambda");
ui->pushButton1->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton1->setText("FEEDER ON");
});
QTimer::singleShot(1000, [ this]{
qDebug("Hello from lambda");
ui->pushButton2->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton2->setText("FEEDER ON");
}); -
@mrjj this working for me..but
1.if i give same delay value it exicute at the same time
QTimer::singleShot(1000, [ this]{
qDebug("Hello from lambda");
ui->pushButton1->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton1->setText("FEEDER ON");
});
QTimer::singleShot(1000, [ this]{
qDebug("Hello from lambda");
ui->pushButton2->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton2->setText("FEEDER ON");
});
2.if give different time value the it works
QTimer::singleShot(500, [ this]{
qDebug("Hello from lambda");
ui->pushButton1->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton1->setText("FEEDER ON");
});
QTimer::singleShot(1000, [ this]{
qDebug("Hello from lambda");
ui->pushButton2->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton2->setText("FEEDER ON");
});@swansorter said in QT delay not working:
if i give same delay value it exicute at the same time
Sure, because you want the second part to be delayed after the first one, right? So, after 1sec delay you do the first change, then again after 1sec more you do the second part.
-
@mrjj this working for me..but
1.if i give same delay value it exicute at the same time
QTimer::singleShot(1000, [ this]{
qDebug("Hello from lambda");
ui->pushButton1->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton1->setText("FEEDER ON");
});
QTimer::singleShot(1000, [ this]{
qDebug("Hello from lambda");
ui->pushButton2->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton2->setText("FEEDER ON");
});
2.if give different time value the it works
QTimer::singleShot(500, [ this]{
qDebug("Hello from lambda");
ui->pushButton1->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton1->setText("FEEDER ON");
});
QTimer::singleShot(1000, [ this]{
qDebug("Hello from lambda");
ui->pushButton2->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton2->setText("FEEDER ON");
});Hi
Yes it is executed when time is gone.Im not really sure what you are trying but it seems you want more than one button to change
color ? -
Hi
Yes it is executed when time is gone.Im not really sure what you are trying but it seems you want more than one button to change
color ?@mrjj i want like this
1.ui->pushButton1->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton1->setText("FEEDER ON");
delay(1000);
after 1 sec delay
2.
ui->pushButton2->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton2->setText("FEEDER ON");
delay(1000);another 1sec delay
3.
ui->pushButton3->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton3->setText("FEEDER ON") -
@mrjj i want like this
1.ui->pushButton1->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton1->setText("FEEDER ON");
delay(1000);
after 1 sec delay
2.
ui->pushButton2->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton2->setText("FEEDER ON");
delay(1000);another 1sec delay
3.
ui->pushButton3->setStyleSheet("font-size: 20px;""background: green;");
ui->pushButton3->setText("FEEDER ON") -
Hi,
I thing is not a good idea to use Qthread::msleep in this way for GUI, as someone has already suggested you I should use Qtimer::singleShot.Only for test, have you tried with update function before msleep?
ui->pushButtonSystemswitchon->setStyleSheet("font-size: 20px;" "background: red;"); ui->pushButtonSystemswitchon->setText("ON"); ui->pushButtonFeeder->setEnabled(false); ui→pushButtonEejecter→setEnabled(false);
ui→pushButtonSystemswitchon→update();
ui→pushButtonSystemswitchon→update();
ui→pushButtonFeeder→update();
ui→pushButtonEejecter→update();QThread::msleep(1000);
-
@mrjj am asking is their any other way ?
-
@mrjj am asking is their any other way ?
@swansorter Why do you need other way? What is wrong with QTimer::singleShot and delays of 1000 and 2000?
-
@swansorter Why do you need other way? What is wrong with QTimer::singleShot and delays of 1000 and 2000?
@jsulm i dnt know weather it is correct way or not
-
@mrjj thank you very much