if (Q_UNLIKELY(interval < 0)) { qWarning("QObject::startTimer: Timers cannot have negative intervals"); return0; } if (Q_UNLIKELY(!d->threadData->hasEventDispatcher())) { qWarning("QObject::startTimer: Timers can only be used with threads started with QThread"); return0; } if (Q_UNLIKELY(thread() != QThread::currentThread())) { qWarning("QObject::startTimer: Timers cannot be started from another thread"); return0; } int timerId = d->threadData->eventDispatcher.load()->registerTimer(interval, timerType, this); if (!d->extraData) d->extraData = new QObjectPrivate::ExtraData; d->extraData->runningTimers.append(timerId); return timerId; }
bool ok = false; calculateNextTimeout(t, qt_msectime()); uint interval = t->interval; if (interval == 0u) { // optimization for single-shot-zero-timer QCoreApplication::postEvent(q, newQZeroTimerEvent(t->timerId)); ok = true; } elseif (interval < 20u || t->timerType == Qt::PreciseTimer) { // 3/2016: Although MSDN states timeSetEvent() is deprecated, the function // is still deemed to be the most reliable precision timer. t->fastTimerId = timeSetEvent(interval, 1, qt_fast_timer_proc, DWORD_PTR(t), TIME_CALLBACK_FUNCTION | TIME_PERIODIC | TIME_KILL_SYNCHRONOUS); ok = t->fastTimerId; }
if (!ok) { // user normal timers for (Very)CoarseTimers, or if no more multimedia timers available ok = SetTimer(internalHwnd, t->timerId, interval, 0); }
if (!ok) qErrnoWarning("QEventDispatcherWin32::registerTimer: Failed to create a timer"); }
熟悉的 Windows API 就被发现了。如果当初我能记得 QObject 里边的调用细节,或许应该就在面试的让对方少些疑问了。