forked from benlau/quickpromise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqptimer.cpp
More file actions
69 lines (48 loc) · 1.51 KB
/
qptimer.cpp
File metadata and controls
69 lines (48 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
#include <QtQml>
#include <QVariant>
#include <QTimer>
#include "qptimer.h"
QPTimer::QPTimer(QObject *parent) : QObject(parent)
{
}
QPTimer::~QPTimer()
{
}
void QPTimer::setTimeout(QJSValue func, int interval)
{
// It can't use the Timer from Quick Component to implement the function.
// Because setTimeout(0) could not be executed in next tick with < Qt 5.4
QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),
this,SLOT(onTriggered()),Qt::QueuedConnection);
QVariant v = QVariant::fromValue<QJSValue>(func);
timer->setProperty("__quick_promise_func___",v);
timer->setInterval(interval);
timer->setSingleShot(true);
timer->start();
}
void QPTimer::onTriggered()
{
QTimer* timer = qobject_cast<QTimer*>(sender());
QJSValue func = timer->property("__quick_promise_func___").value<QJSValue>();
QJSValue res = func.call();
if (res.isError()) {
qDebug() << "Q.setTimeout() - Uncaught exception at line"
<< res.property("lineNumber").toInt()
<< ":" << res.toString();
}
timer->deleteLater();
}
static QJSValue provider(QQmlEngine* engine , QJSEngine *scriptEngine) {
Q_UNUSED(engine);
QPTimer* timer = new QPTimer();
QJSValue value = scriptEngine->newQObject(timer);
return value;
}
class QPTimerRegisterHelper {
public:
QPTimerRegisterHelper() {
qmlRegisterSingletonType("QuickPromise", 1, 0, "QPTimer", provider);
}
};
static QPTimerRegisterHelper registerHelper;