aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/tests
diff options
context:
space:
mode:
authorAdrian Herrmann <adrian.herrmann@qt.io>2024-01-31 18:03:36 +0100
committerAdrian Herrmann <adrian.herrmann@qt.io>2024-02-13 13:28:49 +0100
commit194e040570d4d3727b983d93b0e7a4957348a27c (patch)
treeec962fdaa7ff4fe0fffff7c506b9cf9778e5602b /sources/pyside6/tests
parentb806c5e75e89fbe0a3fc53eb82ca1b27524cca69 (diff)
QTimer: Call C++ function for singleShot timers
The current implementation of singleshot timers is very old and bypasses the C++ function. Instead, a timer object is manually created and started. This incurs a performance penalty, as this bypasses the optimized code path for 0 timers that eschews a timer object in favor of directly calling QMetaObject::invokeMethod. This is now fixed, and for 0 timers, the C++ function is called directly. Change-Id: Idfed06d60eb34355242818ac2df46f75dd27353c Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources/pyside6/tests')
-rw-r--r--sources/pyside6/tests/QtCore/qtimer_singleshot_test.py64
1 files changed, 63 insertions, 1 deletions
diff --git a/sources/pyside6/tests/QtCore/qtimer_singleshot_test.py b/sources/pyside6/tests/QtCore/qtimer_singleshot_test.py
index 9718a2427..2ccaa300e 100644
--- a/sources/pyside6/tests/QtCore/qtimer_singleshot_test.py
+++ b/sources/pyside6/tests/QtCore/qtimer_singleshot_test.py
@@ -14,7 +14,7 @@ sys.path.append(os.fspath(Path(__file__).resolve().parents[1]))
from init_paths import init_test_paths
init_test_paths(False)
-from PySide6.QtCore import QObject, QThread, QTimer, Signal
+from PySide6.QtCore import QObject, QThread, QTimer, Signal, Slot, SLOT
from helper.usesqapplication import UsesQApplication
@@ -74,6 +74,11 @@ class TestSingleShot(UsesQApplication):
self.app.exec()
self.assertTrue(self.called)
+ def testSingleShotZero(self):
+ QTimer.singleShot(0, self.callback)
+ self.app.exec()
+ self.assertTrue(self.called)
+
def testSingleShotWithContext(self):
thread = ThreadForContext()
thread.start()
@@ -97,6 +102,56 @@ class TestSingleShot(UsesQApplication):
self.assertEqual(self.qthread, thread.qthread)
+class TestSingleShotCallableObject(UsesQApplication):
+ '''Test case for QTimer.singleShot with callable inside an object'''
+
+ def setUp(self):
+ # Acquire resources
+ UsesQApplication.setUp(self)
+ self.watchdog = WatchDog(self)
+
+ def tearDown(self):
+ # Release resources
+ del self.watchdog
+ # PYSIDE-535: Need to collect garbage in PyPy to trigger deletion
+ gc.collect()
+ UsesQApplication.tearDown(self)
+
+ class CallbackObject(QObject):
+ def __init__(self, app) -> None:
+ super().__init__()
+ self.app = app
+
+ @Slot()
+ def func(self):
+ self.called = True
+ self.app.quit()
+
+ def testSingleShotWithObjectAndMember(self):
+ callback = self.CallbackObject(self.app)
+ QTimer.singleShot(100, callback, SLOT("func()"))
+ self.app.exec()
+ self.assertTrue(callback.called)
+
+ def testSingleShotWithObjectAndMemberZero(self):
+ callback = self.CallbackObject(self.app)
+ QTimer.singleShot(0, callback, SLOT("func()"))
+ self.app.exec()
+ self.assertTrue(callback.called)
+
+ def testSingleShotWithCallableInObject(self):
+ callback = self.CallbackObject(self.app)
+ QTimer.singleShot(100, callback.func)
+ self.app.exec()
+ self.assertTrue(callback.called)
+
+ def testSingleShotWithCallableInObjectZero(self):
+ callback = self.CallbackObject(self.app)
+ QTimer.singleShot(0, callback.func)
+ self.app.exec()
+ self.assertTrue(callback.called)
+
+
class SigEmitter(QObject):
sig1 = Signal()
@@ -128,6 +183,13 @@ class TestSingleShotSignal(UsesQApplication):
self.app.exec()
self.assertTrue(self.called)
+ def testSingleShotSignalZero(self):
+ emitter = SigEmitter()
+ emitter.sig1.connect(self.callback)
+ QTimer.singleShot(0, emitter.sig1)
+ self.app.exec()
+ self.assertTrue(self.called)
+
if __name__ == '__main__':
unittest.main()