From 521aa7f562a5f215de09e3f37168f6a6d0ce5543 Mon Sep 17 00:00:00 2001 From: Andrew den Exter Date: Tue, 10 Jan 2012 10:28:11 +1000 Subject: [PATCH] Fix restarting timer from onTriggered handler. Set the running property to false before calling the triggered handler when a timer finishes so it does not appear to still be running and can be restarted by setting the running property to true. Task-number: QTBUG-22004 Change-Id: I840efa30f5b7ad7d0cda96803d4898be3f390705 Reviewed-by: Martin Jones --- src/quick/util/qdeclarativetimer.cpp | 2 +- .../qdeclarativetimer/tst_qdeclarativetimer.cpp | 59 ++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/quick/util/qdeclarativetimer.cpp b/src/quick/util/qdeclarativetimer.cpp index 3855521..e030947 100644 --- a/src/quick/util/qdeclarativetimer.cpp +++ b/src/quick/util/qdeclarativetimer.cpp @@ -315,9 +315,9 @@ void QDeclarativeTimer::finished() Q_D(QDeclarativeTimer); if (d->repeating || !d->running) return; - emit triggered(); d->running = false; d->firstTick = false; + emit triggered(); emit runningChanged(); } diff --git a/tests/auto/qtquick2/qdeclarativetimer/tst_qdeclarativetimer.cpp b/tests/auto/qtquick2/qdeclarativetimer/tst_qdeclarativetimer.cpp index 6582f0f..b7045a1 100644 --- a/tests/auto/qtquick2/qdeclarativetimer/tst_qdeclarativetimer.cpp +++ b/tests/auto/qtquick2/qdeclarativetimer/tst_qdeclarativetimer.cpp @@ -61,6 +61,8 @@ private slots: void triggeredOnStartRepeat(); void changeDuration(); void restart(); + void restartFromTriggered(); + void runningFromTriggered(); void parentProperty(); }; @@ -314,6 +316,63 @@ void tst_qdeclarativetimer::restart() delete timer; } +void tst_qdeclarativetimer::restartFromTriggered() +{ + QDeclarativeEngine engine; + QDeclarativeComponent component(&engine); + component.setData(QByteArray("import QtQuick 2.0\nTimer { " + "interval: 500; " + "repeat: false; " + "running: true; " + "onTriggered: restart()" + " }"), QUrl::fromLocalFile("")); + QScopedPointer object(component.create()); + QDeclarativeTimer *timer = qobject_cast(object.data()); + QVERIFY(timer != 0); + + TimerHelper helper; + connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout())); + QCOMPARE(helper.count, 0); + + QTest::qWait(600); + QCOMPARE(helper.count, 1); + QVERIFY(timer->isRunning()); + + QTest::qWait(600); + QCOMPARE(helper.count, 2); + QVERIFY(timer->isRunning()); +} + +void tst_qdeclarativetimer::runningFromTriggered() +{ + QDeclarativeEngine engine; + QDeclarativeComponent component(&engine); + component.setData(QByteArray("import QtQuick 2.0\nTimer { " + "property bool ok: false; " + "interval: 500; " + "repeat: false; " + "running: true; " + "onTriggered: { ok = !running; running = true }" + " }"), QUrl::fromLocalFile("")); + QScopedPointer object(component.create()); + QDeclarativeTimer *timer = qobject_cast(object.data()); + QVERIFY(timer != 0); + + TimerHelper helper; + connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout())); + QCOMPARE(helper.count, 0); + + QTest::qWait(600); + QCOMPARE(helper.count, 1); + QVERIFY(timer->property("ok").toBool()); + QVERIFY(timer->isRunning()); + + QTest::qWait(600); + QCOMPARE(helper.count, 2); + QVERIFY(timer->property("ok").toBool()); + QVERIFY(timer->isRunning()); +} + void tst_qdeclarativetimer::parentProperty() { QDeclarativeEngine engine; -- 2.7.4