swift
Loading...
Searching...
No Matches
threadedtimer.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2025 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <QPointer>
7
8#include "threadutils.h"
9
10namespace swift::misc
11{
13 {
14 m_updateTimer.setObjectName(name + ":timer");
15 m_updateTimer.setSingleShot(singleShot);
16 connect(&m_updateTimer, &QTimer::timeout, this, &CThreadedTimer::timeout);
17 }
18
19 void CThreadedTimer::startTimer(std::chrono::milliseconds ms)
20 {
22 {
23 // shift in correct thread
24 QPointer<CThreadedTimer> myself(this);
25 QTimer::singleShot(0, this, [=, this] {
26 if (!myself)
27 {
28 return;
29 }
30 this->startTimer(ms);
31 });
32 return;
33 }
34
36 m_updateTimer.start(ms);
37 }
38
40 {
42 {
43 // shift in correct thread
44 QPointer<CThreadedTimer> myself(this);
45 QTimer::singleShot(0, this, [=, this] {
46 if (!myself)
47 {
48 return;
49 }
50 this->stopTimer();
51 });
52 return;
53 }
54
55 if (!m_updateTimer.isActive())
56 {
57 return;
58 }
59 m_updateTimer.stop();
60 }
61} // namespace swift::misc
static bool isInThisThread(const QObject *toBeTested)
Is the current thread the object's thread?
void stopTimer()
Safely stop update time.
CThreadedTimer(QObject *owner, const QString &name, bool singleShot=false)
Constructor.
void startTimer(std::chrono::milliseconds ms)
Start updating (start timer).
void timeout()
Timer timed out.
Free functions in swift::misc.
auto singleShot(int msec, QObject *target, F &&task)
Starts a single-shot timer which will call a task in the thread of the given object when it times out...
Definition threadutils.h:30
QObject(QObject *parent)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QThread * thread() const const
UniqueConnection
void finished()
void stop()
void timeout()