Reputation: 22946
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?
Upvotes: 1
Views: 9673
Reputation: 92647
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
Upvotes: 1
Reputation: 4728
objTime->addSecs(-1)
doesn't change objTime but returns a new QTime object that is one second less than objTime.
Then .minute()
returns the minute part of the new QTime object and .second()
returns the second part of the new QTime object.
The code could be more efficient if it pre-calculates the new time only once.
Also the line could be rewritten as (but the new code below takes into consideration the hours too):
*objTime = objTime->addSecs(-1); // Thanks André Caron for the correction
without using setHMS()
Upvotes: 1
Reputation: 17234
Use a QTimer class if you want a count down timer kind of functionality.
Upvotes: 3