This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-Qt/doric/engine/DoricTimerExtension.cpp

37 lines
881 B
C++
Raw Normal View History

2021-04-06 17:58:23 +08:00
#include <QCoreApplication>
2021-02-04 16:59:58 +08:00
#include <QTimer>
#include "../utils/DoricConstant.h"
#include "DoricTimerExtension.h"
Q_INVOKABLE void DoricTimerExtension::setTimer(long timerId, int time,
bool repeat) {
2021-04-06 17:58:23 +08:00
connect(this, &DoricTimerExtension::startTimer, qApp, [=]() {
QTimer *timer = new QTimer();
timer->setSingleShot(!repeat);
connect(timer, &QTimer::timeout, [=]() {
if (deletedTimerIds->contains(timerId)) {
2021-02-04 16:59:58 +08:00
deletedTimerIds->remove(timerId);
2021-04-06 17:58:23 +08:00
timer->deleteLater();
} else {
this->method(timerId);
if (!repeat) {
deletedTimerIds->remove(timerId);
timer->deleteLater();
}
2021-02-04 16:59:58 +08:00
}
2021-04-06 17:58:23 +08:00
});
timer->start(time);
2021-02-04 16:59:58 +08:00
});
2021-04-06 17:58:23 +08:00
emit startTimer();
2021-02-04 16:59:58 +08:00
}
Q_INVOKABLE void DoricTimerExtension::clearTimer(long timerId) {
deletedTimerIds->insert(timerId);
}