View Full Version : sleep - sleeps, but not when I want it to sleep

2nd March 2006, 08:16
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);

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?

2nd March 2006, 08:57
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.

2nd March 2006, 14:43
Thanks a lot.