How to use QTimer inside for loop
-
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--; }
-
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() -
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 -
@illyaSlobozhanin
Ok :)
please set as "Solved" using the Topic Tools Button at first post :) -
@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. -
@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 secsbut really not sure :)