Results 1 to 5 of 5

Thread: Help about sleep on C++ ...

  1. #1
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Help about sleep on C++ ...

    I wanted with a push button the Pixmap of a label change.. so i had write this.

    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. ui->label->setPixmap(QPixmap(QString::fromUtf8(":/new/prefix1/1.gif")));
    4. }
    To copy to clipboard, switch view to plain text mode 

    It works...

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

    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. ui->label->setPixmap(QPixmap(QString::fromUtf8(":/new/prefix1/1.gif")));
    4. system("sleep 1");
    5. ui->label->setPixmap(QPixmap(QString::fromUtf8(":/new/prefix1/2.jpg")));
    6. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Help about sleep on C++ ...

    You have to use a QTimer. Connect its timeout signal with a slot where you set back the image.

  3. #3
    Join Date
    Dec 2007
    Posts
    27
    Thanks
    1
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help about sleep on C++ ...

    Quote Originally Posted by Bong.Da.City View Post
    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:
    Qt Code:
    1. t.start();
    2. while(t.elapsed() < 1000) {
    3. QCoreApplication::processEvents(QEventLoop::AllEvents, 1000-t.elapsed());
    4. }
    5. }
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Aug 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Help about sleep on C++ ...

    An you are sure that sleep takes seconds rather than milliseconds....

  5. #5
    Join Date
    Dec 2007
    Posts
    27
    Thanks
    1
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help about sleep on C++ ...

    I assume sleep from the GNU coreutils. This sleep will take seconds as argument.

Similar Threads

  1. How do you sleep when using a QTimer
    By newqtuser in forum Qt Programming
    Replies: 0
    Last Post: 30th October 2008, 00:49
  2. how to sleep with qt function?
    By yleesun in forum Qt Programming
    Replies: 2
    Last Post: 5th August 2008, 12:08
  3. sleep
    By sonuani in forum Newbie
    Replies: 1
    Last Post: 17th March 2008, 12:37
  4. sleep - sleeps, but not when I want it to sleep
    By nongentesimus in forum Newbie
    Replies: 2
    Last Post: 2nd March 2006, 14:43
  5. where's the sleep() func?
    By TheKedge in forum Qt Programming
    Replies: 11
    Last Post: 2nd February 2006, 15:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.