PDA

View Full Version : QTime



dragon
11th September 2007, 16:58
Hello anyone,

I ám a newbie,
I have a question about QTime and QTimer.
I have read the posts in the forum about QTime and Qtimer but it gives me not the solution that i want.

I want to countdown the Time(not the currentTime)in lcdNumber. Example(05:00:00->04:59:59...)
Is it possible to do it in QTime ?.


See my implentation:


countdownDialog::countdownDialog(QWidget *parent)
:QDialog(parent)

{

setupUi(this);


QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(displayTime()));
timer->start(1000);

}
void countdownDialog::displayTime()
{
QTime *time = new QTime();
time->setHMS(5,0,0,0);
lcdNumber->display(time->toString());

}

Thanks in advance.

jpn
11th September 2007, 19:13
Let's start with fixing an obvious memory leak. Here "time" is allocated on the heap but never deleted:



void countdownDialog::displayTime()
{
QTime *time = new QTime();
time->setHMS(5,0,0,0);
lcdNumber->display(time->toString());
}

Just for the learning purposes, you should start with rewriting it as:


void countdownDialog::displayTime()
{
QTime time;
time.setHMS(5,0,0,0);
lcdNumber->display(time.toString());
}

Now, "time" get automatically destructed while going out of scope.

The next thing is to actually implement some counter functionality. All the above code does is converting the same time (05:00:00) over and over again to a string and setting it to the lcd number widget. You could make the time object a member variable instead and reduce a second every iteration:


class countdownDialog
{
...
private:
QTime time; // declare a member variable
}

countdownDialog::countdownDialog(QWidget *parent)
:QDialog(parent)
{
...
// initialize it to anything you want,
// for example 5 hours as you did before in displayTime()
time = QTime(5, 0, 0, 0);
}

void countdownDialog::displayTime()
{
time = time.addSecs(-1); // reduce a second
lcdNumber->display(time.toString());
}

dragon
11th September 2007, 20:13
Thanks J-P Nurmi for advise its works fine now.

wysota
11th September 2007, 22:02
A bit more precise solution is to substract the elapsed time from 5 hours every time. It may happen that a timer event gets stalled, so substracting a second every time it is triggered might get your timer out of sync. But if you don't expect a heavy load, it should work fine too.

dragon
12th September 2007, 19:35
I have a question i only see the hours, minutes, seconds in the lcdNumber.
I.don't see the milliseconds.
Is this depending on the system where the program is running on.
How can i change the code to see the milliseconds and counting down in lcdNumber.

thanks in advance.

marcel
12th September 2007, 19:44
instead of time.toString() you can build your own representation by using QTime::hour(), minute(), second() and msec().
Just convert them to QString and concatenate them.

Regards

marcel
12th September 2007, 19:45
:)
That, or:


time.toString("hh:mm:ss.zzz");

There are other formatters from which you can choose.

Regards

dragon
12th September 2007, 20:31
I see in my lcdNumber the milliseconds but is not counting down only the seconds is counting down.
Must i time.addMSecs to the code?

wysota
12th September 2007, 20:43
You are only reducing the number of seconds so how do you expect anything else to change?

marcel
12th September 2007, 20:44
Yes, addMsecs(-x), where x is smaller than 1000, in order to see a difference.

Regards

dragon
12th September 2007, 20:57
Thanks Marcel and Wysota for your advise.

wysota
12th September 2007, 21:15
Or try my approach :)

arninio123
16th February 2012, 10:40
hello.

is it possible to add a start, reset and pause button

but already thank you for what i see here :-)