[SOLVED] QTimer delay
-
Hello... I am trying to add delay in my function... I know about thread, but I don't want application to freeze... I also know about QTimer but I can not find anything usefull there, because I am not calling any slot...
For example, if P1C1->isChecked, on_P1C1Clientless_clicked() should be executed and then there must be lets say 5 second delay, before function continue to next if statement... Application must not be frozen and must allow user to do other things while function loginP1() is executing...
@void MainWindow::loginP1()
{
QTimer *timer = new QTimer();loginC = true;
if(ui->P1C1->isChecked())
{
on_P1C1Clientless_clicked();
//Add timer delay
}
if(ui->P1C2->isChecked())
{
on_P1C2Clientless_clicked();
//Add timer delay
}
if(ui->P1C3->isChecked())
{
on_P1C3Clientless_clicked();
//Add timer delay
}
if(ui->P1C4->isChecked())
{
on_P1C4Clientless_clicked();
//Add timer delay
}
if(ui->P1C5->isChecked())
{
on_P1C5Clientless_clicked();
//Add timer delay
}
if(ui->P1C6->isChecked())
{
on_P1C6Clientless_clicked()
//Add timer delay
}
if(ui->P1C7->isChecked())
{
on_P1C7Clientless_clicked();
//Add timer delay
}
if(ui->P1C8->isChecked())
{
on_P1C8Clientless_clicked();
//Add timer delay
}@ -
Hi,
You could have each test in a slot and use QTimer's singleShot to go from one to the next.
-
You have two choice
- Making it using the singleshot timer. This is more elegant
- Using procedural programming with QTimer
I have put both codes. Choice is yours
@MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
tim = new QTimer;
tim->setInterval(5000);
tim->start();
check = 0;
//connect(tim,SIGNAL(timeout()),this,SLOT(mytimer()));
QTimer::singleShot(5000,this,SLOT(checkpc1()));
}void MainWindow::checkpc1(){
qDebug() << "PC1 Checked" <<endl;
QTimer::singleShot(5000,this,SLOT(checkpc2()));
}
void MainWindow::checkpc2(){
qDebug() << "PC2 Checked" <<endl;
QTimer::singleShot(5000,this,SLOT(checkpc3()));
}
void MainWindow::checkpc3(){
qDebug() << "PC3 Checked" <<endl;
QTimer::singleShot(5000,this,SLOT(checkpc3()));
}void MainWindow::mytimer(){
if (check == 0 ){
check = 1;
tim->start();
qDebug() << "zero IF" <<endl;
return;
}
if (check == 1 ){
check = 2;
tim->start();
qDebug() << "1 IF" <<endl;
return;
}
if (check == 2 ){
check = 3;
tim->start();
qDebug() << "2 IF" <<endl;
return;
}
if (check == 3 ){
check = 4;
tim->start();
qDebug() << "3 IF" <<endl;
return;
}
}@ -
There's a problem with tim. You don't set it as single shot so it will continue to fire even after all the initialization has been done and also it doesn't fulfill the predicament of first the init stuff and then wait for five seconds
-
In the second case you have to do tim.stop().. do all your work and tim.start(). Offcourse connect statement has to be enabled in the above code.
As I said singleshot as you suggested is best one.
-
Solved problem with QtConcurrent... Works perfect...
@QFuture<void> func = QtConcurrent::run(this, &MainWindow::loginP1);@
-
How does QtConcurrent solve this ?
-
The QtConcurrent::run() function runs a function in a separate thread... loginP1 is executing in separate thread where I can use Sleep() and my application does not freeze...
When button is clicked I call function login(), and login call function loginP1 in separate thread...
@void MainWindow::login()
{
QFuture<void> func = QtConcurrent::run(this, &MainWindow::loginP1);
}@@void MainWindow::loginP1()
{
loginC = true;
if(ui->P1C1->isChecked())
{
on_P1C1Clientless_clicked();
Sleep(delay);
}
if(ui->P1C2->isChecked())
{
on_P1C2Clientless_clicked();
Sleep(delay);
}
if(ui->P1C3->isChecked())
{
on_P1C3Clientless_clicked();
Sleep(delay);
}
if(ui->P1C4->isChecked())
{
on_P1C4Clientless_clicked();
Sleep(delay);
}
if(ui->P1C5->isChecked())
{
on_P1C5Clientless_clicked();
Sleep(delay);
}
if(ui->P1C6->isChecked())
{
on_P1C6Clientless_clicked();
Sleep(delay);
}
if(ui->P1C7->isChecked())
{
on_P1C7Clientless_clicked();
Sleep(delay);
}
if(ui->P1C8->isChecked())
{
on_P1C8Clientless_clicked();
Sleep(delay);
}
loginC = false;
}@