QTimer QLCDNumber Digital Clock
-
Hi,
I refer to the Qt example of the Digital Clock implemented using QTimer and QLCDNumber.
http://harmattan-dev.nokia.com/docs/library/html/qt4/widgets-digitalclock.html
I have implemented my own codes. I wanted the time to start at a pre-defined timing (i.e. 12:00:00) instead of getting the currentTime(). However, when I done the following, the time didn't move.
Please help. Thanks.
This is what I have done:
My .cpp:
@void StatusBar::createButtons()
{
lcdSimulation = new QLCDNumber;
lcdSimulation->display("12:00:00");
simulationTimer = new QTimer;
simulationTimer->start(1000);
QObject::connect(simulationTimer, SIGNAL(timeout()), this, SLOT(tickSimulation()));
addWidget(lcdSimulation);
}void StatusBar::tickSimulation()
{
QTime time = QTime::QTime(11, 0, 0);
QString text = time.toString(Qt::TextDate);if((time.second() % 2) == 0) text[2] = ' '; lcdSimulation->display(text); //works like incrementing the time? time.addSecs(100);
}@
-
Hi, In your StatusBar::tickSimulation you create a LOCAL QTime, not a static one.
so every time you get a 1sec tick, you create a NEW QTime with the 11,0,0 default values. You display that, and then increase the local time variable. This time variable is then destroyed when the function is out of scope again! It is NOT in static memory.
Two (even three options):
First, create a class member and use that as static variable.
of when all instances of the class should use the same value, make the QTime time = QTime::QTime(11, 0, 0) static:
@
static QTime time (11,0,0);
@
Btw, your using a sort of C type declaration with C++ class member association in your declaration of time ;-) -
Do I declare @static QTime time(11, 0, 0);@ inside my StatusBar::tickSimulation()?
@void StatusBar::tickSimulation()
{
static QTime time(11, 0, 0);
QString text = time.toString(Qt::TextDate);if((time.second() % 2) == 0) text[2] = ' '; lcdSimulation->display(text); //works like incrementing the time? time.addSecs(100);
}@
I can compile it but the clock timer does not start. Should I be declaring the static variable in my class or in my constructor instead?