18using namespace std::chrono_literals;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
120
121
123QTimer::QTimer(QObject *parent)
124 : QObject(*
new QTimerPrivate(
this), parent)
126 Q_ASSERT(d_func()->isQTimer);
131
132
136 if (d_func()->isActive())
142
143
144
145
146
147
150
151
152
153
154
155
158
159
160
161
162bool QTimer::isActive()
const
164 return d_func()->isActiveData.value();
167QBindable<
bool> QTimer::bindableActive()
169 return QBindable<
bool>(&d_func()->isActiveData);
173
174
175
176
177
178int QTimer::timerId()
const
180 auto v = qToUnderlying(id());
181 return v == 0 ? -1 : v;
185
186
187
188
189
190
191Qt::TimerId QTimer::id()
const
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
220 Qt::TimerId newId{ QObject::startTimer(d->inter * 1ms, d->type) };
221 if (newId > Qt::TimerId::Invalid) {
223 d->isActiveData.notify();
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246void QTimer::start(
int msec)
251static std::chrono::milliseconds
254 constexpr auto maxInterval = INT_MAX * 1ms;
255 if (interval < 0ms) {
256 qWarning(
"%s: negative intervals aren't allowed; the interval will be set to 1ms.", caller);
258 }
else if (interval > maxInterval) {
259 qWarning(
"%s: interval exceeds maximum allowed interval, it will be clamped to "
260 "INT_MAX ms (about 24 days).", caller);
261 interval = maxInterval;
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287void QTimer::start(std::chrono::milliseconds interval)
291 interval = checkInterval(
"QTimer::start", interval);
292 const int msec = interval.count();
293 const bool intervalChanged = msec != d->inter;
294 d->inter.setValue(msec);
303
304
305
306
312 QObject::killTimer(d->id);
313 d->id = Qt::TimerId::Invalid;
314 d->isActiveData.notify();
320
321
322void QTimer::timerEvent(QTimerEvent *e)
325 if (e->id() == d->id) {
328 emit timeout(QPrivateSignal());
332QAbstractEventDispatcher::Duration
333QTimer::from_msecs(std::chrono::milliseconds ms)
335 using Duration = QAbstractEventDispatcher::Duration;
337 using namespace std::chrono;
338 using ratio = std::ratio_divide<std::milli, Duration::period>;
339 static_assert(ratio::den == 1);
342 if (qMulOverflow<ratio::num>(ms.count(), &r)) {
343 qWarning(
"QTimer::singleShot(std::chrono::milliseconds, ...): "
344 "interval argument overflowed when converted to nanoseconds.");
345 return Duration::max();
351
352
353
354
355
356
357
358
359
360
361void QTimer::singleShotImpl(std::chrono::nanoseconds ns, Qt::TimerType timerType,
362 const QObject *receiver,
363 QtPrivate::QSlotObjectBase *slotObj)
366 bool deleteReceiver =
false;
374 if (!receiver && QThread::currentThread() == QCoreApplicationPrivate::mainThread()) {
376 receiver = QThread::currentThread();
377 }
else if (!receiver) {
380 receiver =
new QObject;
381 deleteReceiver =
true;
384 auto h = QtPrivate::invokeMethodHelper({});
385 QMetaObject::invokeMethodImpl(
const_cast<QObject *>(receiver), slotObj,
386 Qt::QueuedConnection, h.parameterCount(), h.parameters.data(), h.typeNames.data(),
390 const_cast<QObject *>(receiver)->deleteLater();
394 (
void)
new QSingleShotTimer(ns, timerType, receiver, slotObj);
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
441void QTimer::singleShot(std::chrono::nanoseconds ns, Qt::TimerType timerType,
442 const QObject *receiver,
const char *member)
445 qWarning(
"QTimer::singleShot: negative intervals aren't allowed; the "
446 "interval will be set to 1ms.");
449 if (receiver && member) {
452 const char* bracketPosition = strchr(member,
'(');
453 if (!bracketPosition || !(member[0] >=
'0' && member[0] <=
'2')) {
454 qWarning(
"QTimer::singleShot: Invalid slot specification");
457 const auto methodName = QByteArrayView(member + 1,
458 bracketPosition - 1 - member).trimmed();
459 QMetaObject::invokeMethod(
const_cast<QObject *>(receiver), methodName.toByteArray().constData(),
460 Qt::QueuedConnection);
463 (
void)
new QSingleShotTimer(ns, timerType, receiver, member);
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
569
570
571
572
573
574
575
576
577
578
579
580
581
582
585
586
587
588
589
590
591
594
595
596
597
598
599
600
601
602
603
604
607
608
609
610
611
612
613
614
615
616
617void QTimer::setSingleShot(
bool singleShot)
619 d_func()->single = singleShot;
622bool QTimer::isSingleShot()
const
624 return d_func()->single;
627QBindable<
bool> QTimer::bindableSingleShot()
629 return QBindable<
bool>(&d_func()->single);
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650void QTimer::setInterval(
int msec)
652 setInterval(std::chrono::milliseconds{msec});
655void QTimer::setInterval(std::chrono::milliseconds interval)
659 interval = checkInterval(
"QTimer::setInterval", interval);
660 const int msec = interval.count();
661 d->inter.removeBindingUnlessInWrapper();
662 const bool intervalChanged = msec != d->inter.valueBypassingBindings();
663 d->inter.setValueBypassingBindings(msec);
665 QObject::killTimer(d->id);
666 Qt::TimerId newId{ QObject::startTimer(msec * 1ms, d->type) };
667 if (newId > Qt::TimerId::Invalid) {
673 d->id = Qt::TimerId::Invalid;
674 d->isActiveData.notify();
681int QTimer::interval()
const
683 return d_func()->inter;
686QBindable<
int> QTimer::bindableInterval()
688 return QBindable<
int>(&d_func()->inter);
692
693
694
695
696
697
698
699
700
701
702int QTimer::remainingTime()
const
706 using namespace std::chrono;
707 auto remaining = QAbstractEventDispatcher::instance()->remainingTime(d->id);
708 return ceil<milliseconds>(remaining).count();
715
716
717
718
719
720
721
722void QTimer::setTimerType(Qt::TimerType atype)
724 d_func()->type = atype;
727Qt::TimerType QTimer::timerType()
const
729 return d_func()->type;
732QBindable<Qt::TimerType> QTimer::bindableTimerType()
734 return QBindable<Qt::TimerType>(&d_func()->type);
739#include "moc_qtimer.cpp"
~QTimerPrivate() override
static std::chrono::milliseconds checkInterval(const char *caller, std::chrono::milliseconds interval)