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. Waiting x seconds before continuing
QtWS25 Last Chance

Waiting x seconds before continuing

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 1.7k 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.
  • J Offline
    J Offline
    John Green
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      J 1 Reply Last reply
      4
      • Christian EhrlicherC Christian Ehrlicher

        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

        J Offline
        J Offline
        John Green
        wrote on last edited by
        #3

        @Christian-Ehrlicher sorry but which one is the example?

        mrjjM 1 Reply Last reply
        0
        • J John Green

          @Christian-Ehrlicher sorry but which one is the example?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

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

          J 1 Reply Last reply
          3
          • mrjjM mrjj

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

            J Offline
            J Offline
            John Green
            wrote on last edited by
            #5

            @mrjj Hi thanks for the help however this piece of code throws no matching function call error, any ideas why?

            mrjjM 1 Reply Last reply
            0
            • J John Green

              @mrjj Hi thanks for the help however this piece of code throws no matching function call error, any ideas why?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @John-Green
              Did you include
              #include <QTimer>
              ?
              please post the actual error :)

              J 1 Reply Last reply
              0
              • mrjjM mrjj

                @John-Green
                Did you include
                #include <QTimer>
                ?
                please post the actual error :)

                J Offline
                J Offline
                John Green
                wrote on last edited by
                #7

                @mrjj yes I have it included
                error: no matching function for call to 'QTimer::singleShot(int, MainWindow*, MainWindow::on_Button_clicked()::<lambda()>)'
                });
                ^

                mrjjM 1 Reply Last reply
                0
                • J John Green

                  @mrjj yes I have it included
                  error: no matching function for call to 'QTimer::singleShot(int, MainWindow*, MainWindow::on_Button_clicked()::<lambda()>)'
                  });
                  ^

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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 :)

                  J 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @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 :)

                    J Offline
                    J Offline
                    John Green
                    wrote on last edited by
                    #9

                    @mrjj I just used the code you provided and didn't change anything:

                    QTimer::singleShot(1000, this, [this](){
                         this->ui->label->setText("BACK");
                        });
                    
                    mrjjM 1 Reply Last reply
                    0
                    • J John Green

                      @mrjj I just used the code you provided and didn't change anything:

                      QTimer::singleShot(1000, this, [this](){
                           this->ui->label->setText("BACK");
                          });
                      
                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

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

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

                        Hi,

                        To add to @mrjj, which version of Qt are you using ?

                        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
                        1

                        • Login

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