Results 1 to 3 of 3

Thread: sleep - sleeps, but not when I want it to sleep

  1. #1
    Join Date
    Mar 2006
    Posts
    10
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default sleep - sleeps, but not when I want it to sleep

    Hello, I have the following problem:

    I try to write a GUI application for a card game in Qt 4.1. The user puts a card down to the "table", than the computer responses with other cards. It puts "his" cards down too, than decides who won the trick, and clears the table. All this happens so fast, that you cannot see what the computer put. I tried to slow down things, by simply calling a sleep() function inside the drawing function. This is the part of the drawing function which draws the cards of the table:

    for (int i=0; i<table.size(); i++)
    {
    cardLabel* cl=new cardLabel(this);
    cl->tarsit(table.at(i), true, false);
    cl->setGeometry(x+40*i, y, 80, 120);
    cl->show();
    sleep(2);
    }

    However, it does not want to sleep there. It definitely sleeps, but seems to does the sleeping before the whole drawing process, and than draws everything just as fast as before. Do you have any idea why?
    Peter

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: sleep - sleeps, but not when I want it to sleep

    It is because the actual draw occurs in a paintEvent and not when show() is called. Show just posts a paint event to eventqueue and the event will be processed when the flow returns to the queue (in short words -- after the application returns from functions).

    In your case you should use timers. Each card should be "placed" in a timeout() slot for a timer, so you have to divide your function. This is natural for event driven programming.

  3. #3
    Join Date
    Mar 2006
    Posts
    10
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sleep - sleeps, but not when I want it to sleep

    Thanks a lot.

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.