My QTimer is not starting in another slot/function, but it's startin from constructor
Hi everybody,
I am having one problem, i am giving you code can you find out where is problem.
header file:
Code:
{
Q_OBJECT
public:
MainDialog();
private:
int count;
int listSize;
private slots:
void updateImages();
void animateImage();
};
src file:
Code:
MainDialog
::MainDialog() : QDialog(){
labelText->setText("My Dialog ... !");
hLayout->addWidget(startButton);
hLayout->addWidget(quitButton);
mainLayout->addWidget(labelText);
mainLayout->addItem(vspacer);
mainLayout->addLayout(hLayout);
imagesStrList << ":/images/1.png"
<< ":/images/2.png"
<< ":/images/3.png"
<< ":/images/4.png"
<< ":/images/5.png"
<< ":/images/6.png";
count = 0;
listSize = imagesStrList.size();
connect(timer, SIGNAL(timeout()),this, SLOT(updateImages()));
connect(startButton, SIGNAL(clicked()),this, SLOT(animateImage()));
connect(quitButton, SIGNAL(clicked()),this, SLOT(close()));
//timer->start(400); //[COLOR="#0000FF"] it's working[/COLOR]
resize(320,240);
}
void MainDialog::updateImages()
{
qDebug("update");
labelImage
->setPixmap
(QPixmap(imagesStrList.
at(count
++)));
labelImage->show();
if(count>=listSize){
count=0;
}
}
void MainDialog::animateImage()
{
timer->start(400); //[COLOR="#FF0000"] It's not calling timer slot, I have to srart timer in this slot[/COLOR]
for(int i=0; i<10; i++){
qDebug("LOOP : %d",i);
sleep(1);
}
timer->stop();
}
main file:
Code:
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(resource);
MainDialog dlg;
dlg.show();
return a.exec();
}
i have to start my timer in different slot, but it's not calling the timer slot, if i am starting the timer in constructor, it's working fine.
can you tel me where i am doing wrong ?
Re: My QTimer is not starting in another slot/function, but it's startin from constru
Because for loop in animateImage() method is blocking application event loop.
If you want to do animation for a while you have to use second QTimer to stop it. Something like this :
Code:
void MainDialog::animateImage()
{
timer->start(400); //[COLOR="#FF0000"] It's not calling timer slot, I have to srart timer in this slot[/COLOR]
QTimer::singleShot(10000,timer,
SLOT(stop
()));
//stop animation after 10 seconds }
Re: My QTimer is not starting in another slot/function, but it's startin from constru
hi Lesiok,
first of all thanks for suggesting me.
But my problem is, i have to exeucute some busy process there, i dont know how much time exactly it will take to finish.
So, i cant take second timer as single shot.
I am writing down again that part of code.
Code:
void MainDialog::animateImage()
{
timer->start(400); // It's not calling timer slot, I have to srart timer in this slot
some_busy_or_long_process(); // i dont know how much time it will take, may be 10 sec, 5 sec, 12 sec etc,
timer->stop();
}
I have to start timer before execution of that busy process and i have to stop it after finishing that busy process.
can you give me some idea.........?
Re: My QTimer is not starting in another slot/function, but it's startin from constru
Your program cannot service the timeout() signal unless your code returns to the event loop.
You should read this: [wiki=Keeping_the_GUI_Responsive]Keeping the GUI Responsive[/wiki]