PDA

View Full Version : QProgress Bar-Time Measuring



aegis
13th May 2007, 14:29
hi,

how can i make a progress bar count time?
I have an operation that needs 2.5 minutes to complete and i want a progress bar to count this time....

any ideas??:confused:

N.A

marcel
13th May 2007, 16:00
What if you run it on another computer and it will take 3 minutes or 1 minute?
QProgressBar is used to display the completed amount of a certain process.

Anyway, if you really want to do this, us a QTimer and update the progress bar every 10 secs or so. You will know that 2.5 minutes have passed by computing the time difference between when the QTimer started and the QTimer stopped.
Something like this:



void Class::timeoutSlot()
{
int elapsed = mStartTime.secsTo( QTime::currentTime() );
if( elapsed / 60.0f >= 2.5f )
{
//This means 2.5 mins have elapsed
}
else
{
mProgressBar->setValue( mProgressBar->value() + 10 );
}
}


Above mStartTime is a QTime::currentTime created when you start your progress created when, and your QTimer has a 10 seconds timeout with its timeout signal connected to timeoutSlot().

Regards

wysota
13th May 2007, 16:18
I suggest using QTimeLine. You can then easily choose the update period.


QTimeLine *tl = new QTimeLine;
tl->setDuration(150000);
tl->setRange(0, 150); // update every second
tl->setLoopCount(1);
QProgressBar *pb = new QProgressBar;
pb->setRange(0, 150); // the same as for the time line
connect(tl, SIGNAL(frameChanged(int)), pb, SLOT(setValue(int)));
pb->setValue(0);
connect(tl, SIGNAL(finished()), tl, SLOT(deleteLater())); // cleanup
tl->start();

marcel
13th May 2007, 16:39
Yes, better...

aegis
13th May 2007, 16:47
thanks for the answer, the operation virtually lasts for 2,5 minutes {it emulates time} so that's why i wanted it!