diff options
author | Sergio Martins <iamsergio@gmail.com> | 2018-03-05 22:54:57 +0000 |
---|---|---|
committer | Sérgio Martins <sergio.martins@kdab.com> | 2018-03-25 21:17:20 +0000 |
commit | a6d1456458b6e19c850adadb1a81c4a9acbdbe92 (patch) | |
tree | a4d52f18929ee0d04ffd19b48afb578538dcf89f /tests/auto | |
parent | 863c6887495c0bd9ee3a85aa7cd2d997cdc5c93c (diff) |
Add QTimer::connectTo(), a shorthand way of connecting to timeout()
There are a couple of Qt classes where you almost always use the
same signal, for example QTimer::timeout, QPushButton::clicked,
and QAction::triggered.
Simply doing timer.connectTo([]{}) is much more convenient, less
tedious and even fun.
Not overloading connect() as it would be confusing to see the
receiver as first argument.
And not naming it onTimeout, as that's a popular way of doing it in
other frameworks. People would assume you could use on* with any signal.
If we ever have on* it should be all or nothing.
[ChangeLog][QtCore] Added QTimer::connectTo(), a shorthand way of
connecting to the timeout() signal.
Change-Id: Ida57e5442b13d50972ed585c3ea7be07e3d8e8d2
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp b/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp index 2c6d9ea7c0..2d34aceab8 100644 --- a/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp +++ b/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp @@ -75,6 +75,7 @@ private slots: void dontBlockEvents(); void postedEventsShouldNotStarveTimers(); + void connectTo(); }; class TimerHelper : public QObject @@ -1020,5 +1021,31 @@ void tst_QTimer::crossThreadSingleShotToFunctor() delete o; } +void tst_QTimer::connectTo() +{ + QTimer timer; + TimerHelper timerHelper; + timer.setInterval(0); + timer.start(); + + auto context = new QObject(); + + int count = 0; + timer.connectTo([&count] { count++; }); + QMetaObject::Connection connection = timer.connectTo(context, [&count] { count++; }); + timer.connectTo(&timerHelper, &TimerHelper::timeout); + timer.connectTo(&timer, &QTimer::stop); + + + QTest::qWait(100); + QCOMPARE(count, 2); + QCOMPARE(timerHelper.count, 1); + + // Test that connection is bound to context lifetime + QVERIFY(connection); + delete context; + QVERIFY(!connection); +} + QTEST_MAIN(tst_QTimer) #include "tst_qtimer.moc" |