PDA

View Full Version : Help about sleep on C++ ...



Bong.Da.City
24th August 2010, 15:21
I wanted with a push button the Pixmap of a label change.. so i had write this.


void MainWindow::on_pushButton_clicked()
{
ui->label->setPixmap(QPixmap(QString::fromUtf8(":/new/prefix1/1.gif")));
}

It works...

But i want after a second the label change to the previous picture again... So i had put this


void MainWindow::on_pushButton_clicked()
{
ui->label->setPixmap(QPixmap(QString::fromUtf8(":/new/prefix1/1.gif")));
system("sleep 1");
ui->label->setPixmap(QPixmap(QString::fromUtf8(":/new/prefix1/2.jpg")));
}

But it doesn't work... I think what it does is first sleep for one second and then change both pictures.... What should i do?

Lykurg
24th August 2010, 15:41
You have to use a QTimer. Connect its timeout signal with a slot where you set back the image.

yakin
24th August 2010, 15:57
But it doesn't work... I think what it does is first sleep for one second and then change both pictures.... What should i do?

by the way... your code work! But unfortunately you would not see the results. With the system call you block your application. The application has to go back in event processing (main loop) to draw the new image.

You can do something like that instead of the system sleep:


QTime t;
t.start();
while(t.elapsed() < 1000) {
QCoreApplication::processEvents(QEventLoop::AllEve nts, 1000-t.elapsed());
}
}

But in this case, this is techosexual. The suggestion from Lykrug is a much better solution.

przemo_li
24th August 2010, 19:50
An you are sure that sleep takes seconds rather than milliseconds....

yakin
24th August 2010, 20:29
I assume sleep from the GNU coreutils. This sleep will take seconds as argument.