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. Destroying/disconnecting a QTimer initialized in another function
Forum Updated to NodeBB v4.3 + New Features

Destroying/disconnecting a QTimer initialized in another function

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 816 Views 3 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.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on last edited by
    #1

    Hi. I have a QTimer on the heap that is created every time a certain function is called. I would like to make sure I only have one of these QTimers by destroying the previous QTimer every time at the start of that certain function being called. This is how the Timer is made:

    void ApplicationSettings::on_combo_changeBar_activated(int index){
        QTimer *timer = new QTimer(this);
    
        int seconds;
        switch (index){     //Written for redundancy. Future values are probably not gonna be in 2s.
        case 0:
            seconds = 1;
            break;
        case 1:
            seconds = 2;
            break;
        case 2:
            seconds = 4;
            break;
        case 3:
            seconds = 6;
            break;
        case 4:
            seconds = 8;
            break;
        default:
            seconds = 2;
        }
        qDebug() << seconds;
        ui->progressBar->setValue(0);
    
        connect(timer, &QTimer::timeout, this, [=](){this->otherFunction();});
        timer->start(seconds*10);
    }
    

    How can I destroy this QTimer the next time this function is called, such that there will only be one at all times? I wasn't able to find any documentation in the QTimer documentation.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on last edited by
      #2

      Have your timer object be a private member of your class and then do:

      void ApplicationSettings::on_combo_changeBar_activated(int index){
         if (timer == nullptr){// or instantiate it in the constructor
            timer = new QTimer();
            connect(timer, &QTimer::timeout, this, [=](){this->otherFunction();});
         }
         if (timer->isActive()){
            timer->stop();
         }
         .
         .
         .
         timer->start(seconds*10);
      }
      Dummie1138D 1 Reply Last reply
      4
      • M Offline
        M Offline
        mchinand
        wrote on last edited by
        #3

        Also, note that the start method parameter is in milliseconds and there are 1000 milliseconds in a second.

        1 Reply Last reply
        0
        • M mchinand

          Have your timer object be a private member of your class and then do:

          void ApplicationSettings::on_combo_changeBar_activated(int index){
             if (timer == nullptr){// or instantiate it in the constructor
                timer = new QTimer();
                connect(timer, &QTimer::timeout, this, [=](){this->otherFunction();});
             }
             if (timer->isActive()){
                timer->stop();
             }
             .
             .
             .
             timer->start(seconds*10);
          }
          Dummie1138D Offline
          Dummie1138D Offline
          Dummie1138
          wrote on last edited by Dummie1138
          #4

          @mchinand Thank you very much for the response. I am aware that I can declare my timer as part of my class, but I was wondering whether there is a way to simply destroy the timer when the function is redeclared.

          This function is meant to be declared every 20-80 mills, btw.

          Christian EhrlicherC JonBJ 2 Replies Last reply
          0
          • Dummie1138D Dummie1138

            @mchinand Thank you very much for the response. I am aware that I can declare my timer as part of my class, but I was wondering whether there is a way to simply destroy the timer when the function is redeclared.

            This function is meant to be declared every 20-80 mills, btw.

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Dummie1138 said in Destroying/disconnecting a QTimer initialized in another function:

            when the function is redeclared.

            What does this mean?

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

            1 Reply Last reply
            3
            • Dummie1138D Dummie1138

              @mchinand Thank you very much for the response. I am aware that I can declare my timer as part of my class, but I was wondering whether there is a way to simply destroy the timer when the function is redeclared.

              This function is meant to be declared every 20-80 mills, btw.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Dummie1138 said in Destroying/disconnecting a QTimer initialized in another function:

              whether there is a way to simply destroy the timer when the function is redeclared.

              As @Christian-Ehrlicher says, we do not know what "redeclared" means here.

              Assuming you mean when this function is called again. To destroy (or reset) the timer in the function you will need a variable which refers to the timer. You cannot have a function local variable retain its value between calls (don't use static here). So you will need to store that reference variable outside the function somewhere. That is why it would need to be a class variable.

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

                Hi,

                In addition to my fellows, are you looking to just restart that timer ?
                If so, then @mchinand solution is the most simple and effective beside having your QTimer as object member rather than pointer.

                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

                • Login

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