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
QtWS25 Last Chance

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 19 Dec 2015, 12:11 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 19 Dec 2015, 12:14 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 19 Dec 2015, 12:24
      0
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 19 Dec 2015, 12:21 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
        • M mrjj
          19 Dec 2015, 12:14

          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 19 Dec 2015, 12:24 last edited by illyaSlobozhanin
          #4

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

          M 1 Reply Last reply 19 Dec 2015, 12:26
          1
          • I illyaSlobozhanin
            19 Dec 2015, 12:24

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

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 19 Dec 2015, 12:26 last edited by
            #5

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

            I 1 Reply Last reply 19 Dec 2015, 12:27
            0
            • M mrjj
              19 Dec 2015, 12:26

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

              I Offline
              I Offline
              illyaSlobozhanin
              wrote on 19 Dec 2015, 12:27 last edited by
              #6

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

              M 1 Reply Last reply 19 Dec 2015, 12:37
              0
              • I illyaSlobozhanin
                19 Dec 2015, 12:27

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

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 19 Dec 2015, 12:37 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 19 Dec 2015, 12:57
                1
                • M mrjj
                  19 Dec 2015, 12:37

                  @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 19 Dec 2015, 12:57 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?

                  M 1 Reply Last reply 19 Dec 2015, 13:14
                  0
                  • I illyaSlobozhanin
                    19 Dec 2015, 12:57

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

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 19 Dec 2015, 13:14 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 19 Dec 2015, 13:27
                    0
                    • M mrjj
                      19 Dec 2015, 13:14

                      @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 19 Dec 2015, 13:27 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

                      9/10

                      19 Dec 2015, 13:14

                      • Login

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