Let's start with fixing an obvious memory leak. Here "time" is allocated on the heap but never deleted:
Originally Posted by
dragon
void countdownDialog::displayTime()
{
time->setHMS(5,0,0,0);
lcdNumber->display(time->toString());
}
void countdownDialog::displayTime()
{
QTime *time = new QTime();
time->setHMS(5,0,0,0);
lcdNumber->display(time->toString());
}
To copy to clipboard, switch view to plain text mode
Just for the learning purposes, you should start with rewriting it as:
void countdownDialog::displayTime()
{
time.setHMS(5,0,0,0);
lcdNumber->display(time.toString());
}
void countdownDialog::displayTime()
{
QTime time;
time.setHMS(5,0,0,0);
lcdNumber->display(time.toString());
}
To copy to clipboard, switch view to plain text mode
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
){
...
// 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());
}
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());
}
To copy to clipboard, switch view to plain text mode
Bookmarks