Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to use QTimer in right way ?
Forum Updated to NodeBB v4.3 + New Features

How to use QTimer in right way ?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
5 Posts 3 Posters 9.3k Views 1 Watching
  • 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.
  • H Offline
    H Offline
    Hung Tran
    wrote on last edited by
    #1

    Hi,
    I'm new with Qt and use QTimer but It does not work as my expecting. My issue is:
    I have a button. When the button is activated, It will show a QWidget and start countdown timer 8 second to hide the QWidget. However, I want restart countdown timer to 8 second if the button is activated again and countdown timer is less than 8 second.
    This is my code:

    void trigger(QString message){
                  unsigned int timeout = 8000;
                  ui->l_Info->setText(message);
                  ui->l_Info->move(0, 0);
                  ui->l_Info->show();
                  QTimer::singleShot(timeout, this, SLOT(hideMessageAtn()));
    }
    
    void hideMessageAtn(){
                  ui->l_Info->hide();
    }
    

    Actually, this code does work as my expecting. If I activate button in countdown time, it does not reset coundown timer and still emit hideMessageAtn() in the timeout.
    So anyone give me in this case ?
    Thank in advance.

    1 Reply Last reply
    0
    • Vinod KuntojiV Offline
      Vinod KuntojiV Offline
      Vinod Kuntoji
      wrote on last edited by
      #2

      @Hung-Tran ,

      Once the pushbutton is clicked, show the widget, if the timer is already active, stop it, set the interval to 8 seconds and then start the timer. Once after the timeout, hide the widget.

      C++, Qt, Qt Quick Developer,
      PthinkS, Bangalore

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Darkmalex
        wrote on last edited by
        #3

        Hi,
        AFAIK, there is no easy way to reset a QTimer the way you declared it. Your best bet is to create an object of type QTimer, like this

        QTimer *timer = new QTimer(this);
        

        And in your "trigger" function you should do something like this:

        timer->setSingleShot(true);
        timer->start(timeout); //If timer was running, it will restart
        
        1 Reply Last reply
        2
        • Vinod KuntojiV Offline
          Vinod KuntojiV Offline
          Vinod Kuntoji
          wrote on last edited by Vinod Kuntoji
          #4

          @Hung-Tran ,

          myWidget::myWidget()
          {
          timer = new QTimer(this);
          m_Button = new QPushButton("Click");
          widget = new QWidget;
          timer->setSingleShot(true);
          connect(m_Button,SIGNAL(clicked()), this,SLOT(on_pushButton_clicked()));
          connect(timer,SIGNAL(timeout()), this,SLOT(on_TimerTick()));
          }
          void myWidget::on_TimerTick()
          {
          widget ->hide();
          }
          void myWidget::on_pushButton_clicked()
          {
          widget ->show();
          timer->stop();
          timer->setInterval(8000);
          timer->start();
          }

          C++, Qt, Qt Quick Developer,
          PthinkS, Bangalore

          D 1 Reply Last reply
          0
          • Vinod KuntojiV Vinod Kuntoji

            @Hung-Tran ,

            myWidget::myWidget()
            {
            timer = new QTimer(this);
            m_Button = new QPushButton("Click");
            widget = new QWidget;
            timer->setSingleShot(true);
            connect(m_Button,SIGNAL(clicked()), this,SLOT(on_pushButton_clicked()));
            connect(timer,SIGNAL(timeout()), this,SLOT(on_TimerTick()));
            }
            void myWidget::on_TimerTick()
            {
            widget ->hide();
            }
            void myWidget::on_pushButton_clicked()
            {
            widget ->show();
            timer->stop();
            timer->setInterval(8000);
            timer->start();
            }

            D Offline
            D Offline
            Darkmalex
            wrote on last edited by
            #5

            @Vinod-Kuntoji said in How to use QTimer in right way ?:

            ...
            void myWidget::on_pushButton_clicked()
            {
            ...
            timer->stop();
            timer->setInterval(8000);
            timer->start();
            }

            This code is a bit reduntant. simply calling timer->start(8000) will pretty much do the same;

            Here is a quote from Docs (link):

            Starts or restarts the timer with the timeout specified in interval.
            If the timer is already running, it will be stopped and restarted.
            If singleShot is true, the timer will be activated only once.

            1 Reply Last reply
            2

            • Login

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