PDA

View Full Version : how to make program wait



psmech
26th May 2006, 00:36
Hello everyone,

I want to make presentation of sorting algorithms in QT. I've written algorithms for example (only overview):


for( int r=count-1; r>0; --r ){
for( int i=0; i<r; ++i ){
if( elements[i]>elements[i+1] ) exch(elements[i], elements[i+1]);
update();
}
}
In paintEvent function there are rectangles changing it's places after every move - redrawing after every `update()`
But, unfortunately it is going so fast that I can only see the final effect of sorting.

All I want is to WAIT for a second before every `update`. It really doesn't matter if it should be pure C++ or QT code.

Thanks in advance.

jacek
26th May 2006, 01:10
You can't wait in a Qt program, because Qt is event-driven. Event loop must be running, so that GUI can be redrawn.

In your situation the easiest solution is IMO to implement a single iteration of sorting algorithm in a slot and connect that slot to the QTimer::timeout() signal. This way you can control the time between iterations and even implement single-stepping.

michael
26th May 2006, 01:12
Well you could make the function in your class that calls the update() a slot and instead of calling it manually use a QTimer.. See here:
http://doc.trolltech.com/4.1/qtimer.html

EDIT:
Don't you hate it when you post just about the same response as someone else at the same time.. O-Well...

munna
26th May 2006, 06:24
Don't you hate it when you post just about the same response as someone else at the same time.. O-Well...

I posted about this here (http://www.qtcentre.org/forum/showthread.php?t=2336)