Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QTimer delay
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QTimer delay

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 8.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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
    }@

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You could have each test in a slot and use QTimer's singleShot to go from one to the next.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        You have two choice

        1. Making it using the singleshot timer. This is more elegant
        2. 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;
        }
        }@

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

            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.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              Solved problem with QtConcurrent... Works perfect...

              @QFuture<void> func = QtConcurrent::run(this, &MainWindow::loginP1);@

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                How does QtConcurrent solve this ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  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;
                  }@

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved