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. How to use QTimer inside for loop

How to use QTimer inside for loop

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 4.8k 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.
  • I Offline
    I Offline
    illyaSlobozhanin
    wrote on last edited by illyaSlobozhanin
    #1

    Hi all. I've got a problem using QTimer (countdown timer) in for loop

    myClass.h

    class myClass : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit business(QWidget *parent = 0);
        ~myClass();
    
    private slots:
        ....
        void countdownTimer();
        ......
    
    private:
        Ui::business *ui;
        QSqlDatabase db;
        .......
        int sec;
        bool isActive;
        QTimer *timer;
        list<dataClass> list_;
    
        f();
    }
    

    myClass.cpp

    .....
    void myClass::f(){
       list<dataClass>::iterator iter = list_.begin();
       for(; iter != list_.end(); ++iter)
       {
            timer = new QTimer(this); //QTimer *timer; 
            isActive = true;    //bool isActive 
            connect(timer, SIGNAL(timeout()), 
            this, SLOT(countdownTimer()));
            timer->start(1000);
            //some code for waiting while isActive equal false 
            //and than next loop through
           //while(isActive != false);  
           //don't work(infinity loop) 
       }
    }
    
    void myClass::countdownTimer()
    {
           if(seconds < 0) //int seconds = 10 e.g.
           isActive = false;
           seconds--;
    }
    
    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      What is the problem?

      your "void countdownTimer(") don't seem to belong to a class
      and
      connect(timer, SIGNAL(timeout()), this, SLOT(countdownTimer()));
      tries to connect to this->countdownTimer()

      I 1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        If u want to wait for 10 sec and then do something
        Why not just set the timer for 10 secs and when it fires, time has past?
        timer->start(10000); // call after 10 secs

        1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          What is the problem?

          your "void countdownTimer(") don't seem to belong to a class
          and
          connect(timer, SIGNAL(timeout()), this, SLOT(countdownTimer()));
          tries to connect to this->countdownTimer()

          I Offline
          I Offline
          illyaSlobozhanin
          wrote on last edited by illyaSlobozhanin
          #4

          @mrjj it's all ok))) I add some code from .h and .cpp

          mrjjM 1 Reply Last reply
          1
          • I illyaSlobozhanin

            @mrjj it's all ok))) I add some code from .h and .cpp

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

            @illyaSlobozhanin
            Ok :)
            please set as "Solved" using the Topic Tools Button at first post :)

            I 1 Reply Last reply
            0
            • mrjjM mrjj

              @illyaSlobozhanin
              Ok :)
              please set as "Solved" using the Topic Tools Button at first post :)

              I Offline
              I Offline
              illyaSlobozhanin
              wrote on last edited by
              #6

              @mrjj No,no,no. it's ok from your first reply. Problem is in another.

              mrjjM 1 Reply Last reply
              0
              • I illyaSlobozhanin

                @mrjj No,no,no. it's ok from your first reply. Problem is in another.

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

                @illyaSlobozhanin
                Hi
                if you make loops, you stop the message queue and the timer signal may never be sent.
                at least put in a
                while(isActive != false) {
                QApplication::processEvents();
                }
                in the loop to might have it work.

                It would be better to use slot and signal than looping as in
                Qt loops will give you pain.

                I 1 Reply Last reply
                1
                • mrjjM mrjj

                  @illyaSlobozhanin
                  Hi
                  if you make loops, you stop the message queue and the timer signal may never be sent.
                  at least put in a
                  while(isActive != false) {
                  QApplication::processEvents();
                  }
                  in the loop to might have it work.

                  It would be better to use slot and signal than looping as in
                  Qt loops will give you pain.

                  I Offline
                  I Offline
                  illyaSlobozhanin
                  wrote on last edited by illyaSlobozhanin
                  #8

                  @mrjj It doesn't work, unfortunately. It walks through "for" loop only one time.
                  How can i solve this using slot & signal?

                  mrjjM 1 Reply Last reply
                  0
                  • I illyaSlobozhanin

                    @mrjj It doesn't work, unfortunately. It walks through "for" loop only one time.
                    How can i solve this using slot & signal?

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

                    @illyaSlobozhanin said:

                    for(; iter != list_.end(); ++iter)

                    But how many are in the list?
                    It will go though the for loop as many times as there are
                    dataClass in it.

                    If you explain what you are trying to do.
                    seems like
                    for each database
                    wait 10 secs

                    but really not sure :)

                    I 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @illyaSlobozhanin said:

                      for(; iter != list_.end(); ++iter)

                      But how many are in the list?
                      It will go though the for loop as many times as there are
                      dataClass in it.

                      If you explain what you are trying to do.
                      seems like
                      for each database
                      wait 10 secs

                      but really not sure :)

                      I Offline
                      I Offline
                      illyaSlobozhanin
                      wrote on last edited by
                      #10

                      @mrjj There're more than 1 object in list =)
                      It's all ok. i forgot to change variable "isActive" in slot :D
                      Thanks a lot!

                      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