PDA

View Full Version : My QTimer is not starting in another slot/function, but it's startin from constructor



sanjeet
19th November 2011, 05:40
Hi everybody,

I am having one problem, i am giving you code can you find out where is problem.

header file:

class MainDialog : public QDialog
{
Q_OBJECT
public:
MainDialog();
private:
QLabel* labelText;
QLabel* labelImage;
QTimer *timer;
QPushButton* startButton;
QPushButton* quitButton;
QSpacerItem* vspacer;

QStringList imagesStrList;
int count;
int listSize;
private slots:
void updateImages();
void animateImage();
};

src file:

MainDialog::MainDialog() : QDialog()
{
QVBoxLayout *mainLayout = new QVBoxLayout(this);
QHBoxLayout *hLayout = new QHBoxLayout;

labelText = new QLabel;
labelText->setText("My Dialog ... !");

labelImage = new QLabel;

vspacer = new QSpacerItem(20, 150, QSizePolicy::Expanding, QSizePolicy::Minimum);

startButton = new QPushButton("Start");
quitButton = new QPushButton("Quit");

hLayout->addWidget(startButton);
hLayout->addWidget(quitButton);

mainLayout->addWidget(labelText);
mainLayout->addItem(vspacer);
mainLayout->addLayout(hLayout);

timer = new QTimer;
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); // it's working

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); // It's not calling timer slot, I have to srart timer in this slot
for(int i=0; i<10; i++){
qDebug("LOOP : %d",i);
sleep(1);
}
timer->stop();
}

main file:

int main(int argc, char *argv[])
{
QApplication a(argc, 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 ?

Lesiok
19th November 2011, 11:45
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 :

void MainDialog::animateImage()
{
timer->start(400); // It's not calling timer slot, I have to srart timer in this slot
QTimer::singleShot(10000,timer,SLOT(stop()));//stop animation after 10 seconds
}

sanjeet
22nd November 2011, 06:10
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.


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.........?

ChrisW67
22nd November 2011, 07:02
Your program cannot service the timeout() signal unless your code returns to the event loop.

You should read this: Keeping the GUI Responsive