diff options
| author | Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> | 2023-02-23 09:09:50 +0100 |
|---|---|---|
| committer | Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> | 2023-02-27 14:39:19 +0100 |
| commit | 5fac301e8e5a8d15eaff91d78b5deeeb2204c7b4 (patch) | |
| tree | 29a1b8c7ff7ae1c25f9d0661f47f06811c77660b | |
| parent | 78b924368cc4c82a0cb6b0837f00dcf097d24223 (diff) | |
PySide: Add QRunnable create()
Fixes: PYSIDE-2234
Change-Id: I3fc1c669c3985a8aad57785927fb4e48e69431a4
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
| -rw-r--r-- | sources/pyside6/PySide6/QtCore/typesystem_core_common.xml | 3 | ||||
| -rw-r--r-- | sources/pyside6/PySide6/glue/qtcore.cpp | 17 | ||||
| -rw-r--r-- | sources/pyside6/tests/QtCore/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | sources/pyside6/tests/QtCore/qrunnable_test.py | 45 |
4 files changed, 66 insertions, 0 deletions
diff --git a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml index b9c35bfe6..8a90a3fa9 100644 --- a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml +++ b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml @@ -3234,6 +3234,9 @@ <object-type name="QFactoryInterface"/> <object-type name="QRunnable"> <configuration condition="QT_CONFIG(thread)"/> + <add-function signature="create(PyObject* @functionToRun@)" static="yes" return-type="QRunnable*"> + <inject-code class="target" position="beginning" file="../glue/qtcore.cpp" snippet="qrunnable_create"/> + </add-function> </object-type> <object-type name="QPluginLoader"/> diff --git a/sources/pyside6/PySide6/glue/qtcore.cpp b/sources/pyside6/PySide6/glue/qtcore.cpp index 1f9278c4b..9e7d6ad6e 100644 --- a/sources/pyside6/PySide6/glue/qtcore.cpp +++ b/sources/pyside6/PySide6/glue/qtcore.cpp @@ -1767,3 +1767,20 @@ const auto index = mo->indexOfSignal(signature.constData()); const auto result = index != -1 ? mo->method(index) : QMetaMethod{}; %PYARG_0 = %CONVERTTOPYTHON[QMetaMethod](result); // @snippet qmetamethod-from-signal + +// @snippet qrunnable_create +auto callable = %PYARG_1; +auto callback = [callable]() -> void +{ + if (!PyCallable_Check(callable)) { + qWarning("Argument 1 of %FUNCTION_NAME must be a callable."); + return; + } + Shiboken::GilState state; + PyObject_CallObject(callable, nullptr); + Py_DECREF(callable); +}; +Py_INCREF(callable); +%RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(callback); +%PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); +// @snipped qrunnable_create diff --git a/sources/pyside6/tests/QtCore/CMakeLists.txt b/sources/pyside6/tests/QtCore/CMakeLists.txt index 962011fb9..f584bfde6 100644 --- a/sources/pyside6/tests/QtCore/CMakeLists.txt +++ b/sources/pyside6/tests/QtCore/CMakeLists.txt @@ -140,6 +140,7 @@ PYSIDE_TEST(unaryoperator_test.py) PYSIDE_TEST(unicode_test.py) PYSIDE_TEST(versioninfo_test.py) PYSIDE_TEST(loggingcategorymacros_test.py) +PYSIDE_TEST(qrunnable_test.py) if(X11) PYSIDE_TEST(qhandle_test.py) diff --git a/sources/pyside6/tests/QtCore/qrunnable_test.py b/sources/pyside6/tests/QtCore/qrunnable_test.py new file mode 100644 index 000000000..0470a6cd2 --- /dev/null +++ b/sources/pyside6/tests/QtCore/qrunnable_test.py @@ -0,0 +1,45 @@ +# Copyright (C) 2023 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +'''Test cases for QRunnable''' + +import os +import sys +import unittest +from io import StringIO + +from pathlib import Path +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 QCoreApplication, QRunnable, QThreadPool, QThread, qDebug +from helper.usesqcoreapplication import UsesQCoreApplication +test_result = "" + + +def check_test(): + global test_result + test_result = "test works" + + +class QRunnableTest(UsesQCoreApplication): + def testCreateWithAutoDelete(self): + global test_result + test_result = "" # reset + runnable = QRunnable.create(check_test) + runnable.run() + self.assertEqual(test_result, "test works") + + def testwithQThreadPool(self): + global test_result + test_result = "" # reset + runnable = QRunnable.create(check_test) + tp = QThreadPool.globalInstance() + tp.start(runnable) + self.assertTrue(tp.waitForDone()) + self.assertEqual(test_result, "test works") + + +if __name__ == '__main__': + unittest.main() |
