QTime 向新对象添加秒数
QTime add seconds to new object
我使用的是QT5.51。为什么t1无效?:
QTime t1 = QTime().addSecs(122);
qDebug() << t1.isValid() << t1.toString("hh:mm:ss");
我希望得到 "00:02:02" ,但我得到的是错误的 ""。
一个新的默认构造的 QTime
对象以无效状态开始。
QTime::QTime()
Constructs a null time object. A null time can be a QTime(0, 0, 0, 0)
(i.e., midnight) object, except that isNull()
returns true
and isValid()
returns false
.
向无效时间添加秒数会使它无效 - 毕竟,这是一个无效时间点,而不是您似乎期望的午夜。这几乎是一种 NaN 类型的行为。
QTime QTime::addSecs(int s) const
...
Returns a null time if this time is invalid.
要创建处于有效状态的 QTime
,您可以使用其他构造函数
QTime::QTime(int h, int m, int s = 0, int ms = 0)
Constructs a time with hour h, minute m, seconds s and milliseconds ms.
所以午夜初始化的 QTime
将是 QTime(0, 0)
; OP代码应该这样调整:
QTime t1 = QTime(0, 0).addSecs(122);
qDebug() << t1.isValid() << t1.toString("hh:mm:ss");
你也可以使用其他几个helper static methods,这取决于你需要如何初始化它。
我想我明白了:
QTime t1(0,0,0,0);
t1 = t1.addSecs(122);
qDebug() << t1.isValid() << t1.toString("hh:mm:ss");
= true "00:02:02"
我使用的是QT5.51。为什么t1无效?:
QTime t1 = QTime().addSecs(122);
qDebug() << t1.isValid() << t1.toString("hh:mm:ss");
我希望得到 "00:02:02" ,但我得到的是错误的 ""。
一个新的默认构造的 QTime
对象以无效状态开始。
QTime::QTime()
Constructs a null time object. A null time can be a
QTime(0, 0, 0, 0)
(i.e., midnight) object, except thatisNull()
returnstrue
andisValid()
returnsfalse
.
向无效时间添加秒数会使它无效 - 毕竟,这是一个无效时间点,而不是您似乎期望的午夜。这几乎是一种 NaN 类型的行为。
QTime QTime::addSecs(int s) const
...
Returns a null time if this time is invalid.
要创建处于有效状态的 QTime
,您可以使用其他构造函数
QTime::QTime(int h, int m, int s = 0, int ms = 0)
Constructs a time with hour h, minute m, seconds s and milliseconds ms.
所以午夜初始化的 QTime
将是 QTime(0, 0)
; OP代码应该这样调整:
QTime t1 = QTime(0, 0).addSecs(122);
qDebug() << t1.isValid() << t1.toString("hh:mm:ss");
你也可以使用其他几个helper static methods,这取决于你需要如何初始化它。
我想我明白了:
QTime t1(0,0,0,0);
t1 = t1.addSecs(122);
qDebug() << t1.isValid() << t1.toString("hh:mm:ss");
= true "00:02:02"