我認爲,問題是兩個時間戳是不相等的。如果你檢查QDateTime
的==
運營商代碼,你會看到等式成立,如果時間和日期是相等的。
bool QDateTime::operator==(const QDateTime &other) const
{
if (d->spec == other.d->spec && d->utcOffset == other.d->utcOffset)
return d->time == other.d->time && d->date == other.d->date;
else {
QDate date1, date2;
QTime time1, time2;
d->getUTC(date1, time1);
other.d->getUTC(date2, time2);
return time1 == time2 && date1 == date2;
}
}
但是等於操作的時間或比較毫秒:
bool operator==(const QTime &other) const { return mds == other.mds; }
其中mds
是在毫秒的時間。在QTime
構造mds
的計算方法如下:
mds = (h*SECS_PER_HOUR + m*SECS_PER_MIN + s)*1000 + ms;
這將是安全,如果你只是檢查是否兩個時間戳之間的差別在一個限度內。例如:
while (it != _reminders.end() && abs(now.msecsTo(it.key())) < aLimitInMsecs) {
是「它」指向「_reminders.end()」後查找或有效的項目? – Koying 2012-03-06 12:17:39
在進入循環之前,我檢查了'it == _reminders.end()'的條件。這是真的,這就是爲什麼循環從未被執行。但爲什麼? 「QDateTime」的比較運算符可以與此有關嗎? – Pieter 2012-03-06 12:42:45