因此,我在这里找到了可行的方法,并且希望了解它的工作原理。
#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
我希望了解这一行:
objTime->setHMS (0, objTime->addSecs (-1).minute (), objTime->addSecs (-1).second ());
这如何在内部减少分钟和秒钟?
从这里:http://doc.qt.nokia.com/4.7/qtime.html#addSecs
addSecs
函数可能会增加或减少秒数。精细。但这是objTime->addSecs (-1).minute (),
吗?setHMS
如何工作? 最佳答案
此代码示例本身没有像秒表那样倒计时的功能。当前仅显示示例,以显示当前时间值。如果您想让它倒数,我建议您将QTimer添加到类中,并具有1秒的超时时间。您将超时连接到一个插槽,该插槽将从显示时间中减去一秒钟。当您启动内部计时器时,它将每秒触发一次。在执行减法的插槽中,一旦时钟值达到零,就可以停止计时器。
更新
您新编辑的示例正是我在此最初答案中所描述的。计时器每秒触发一次,并调用一个用于调整时间对象的插槽