So, I found something working here, and I wish to understand how it works.
#ifndef LCDNUMBER_H
#define LCDNUMBER_H
#include <QLCDNumber>
#include <QTimer>
#include <QTime>
#include <iostream>
class lcdDisplay : public QLCDNumber
{
Q_OBJECT
public:
// The QTimer class provides repetitive and single-shot timers.
QTimer* objTimer;
// The QTime class provides clock time functions.
QTime* objTime;
public:
lcdDisplay (QWidget *parentWidget, int minutes, int seconds)
{
objTimer = new QTimer ();
// Setting our own time with the specified hours, minutes, and seconds.
objTime = new QTime (0, minutes, seconds);
setParent (parentWidget);
// connect (objectA, signalAFromObjectA, objectB, slotAFromObjectB)
// timeout (): This signal is emitted when the timer times out. The time out period can be specified with `start (int milliseconds)` function.
QObject :: connect (objTimer, SIGNAL (timeout ()), this, SLOT (setDisplay ()));
};
~ lcdDisplay () {};
public slots:
// This slot is called after the timer timeouts (1 second).
void setDisplay ()
{
std::cout << "\nf,gfd,mgnfdm,gnf,\n";
//
objTime->setHMS (0, objTime->addSecs (-1).minute (), objTime->addSecs (-1).second ());
display (objTime->toString ());
};
};
#endif
I wish to understand this line: objTime->setHMS (0, objTime->addSecs (-1).minute (), objTime->addSecs (-1).second ());
How does this decrease the minutes and seconds internally?
From here: http://doc.qt.nokia.com/4.7/qtime.html#addSecs
QTime n(14, 0, 0); // n == 14:00:00
QTime t;
t = n.addSecs(70); // t == 14:01:10
t = n.addSecs(-70); // t == 13:58:50
addSecs function perhaps adds or subtracts seconds. Fine. but does this do objTime->addSecs (-1).minute (),?
How does setHMS work?
This code example by itself has no functionality to count down like a stop watch. Its currently a display only example to show the current time value. If what you want is to allow it to count down, I would recommend adding a QTimer to the class with a 1 second timeout. You connect the timeout to a slot that will subtract one second from your display time. When you start the internal timer it will fire every second. In your slot that does the subtracting you can stop the timer once your clock value reaches zero.
update
Your new edited example is exactly what I was describing here with this initial answer. The timer fires every second and calls a slot that adjusts the time object