diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-06-10 14:07:04 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-06-19 06:48:48 +0000 |
commit | 6feab2b639d32693ad14306529bf72194c1be355 (patch) | |
tree | 0d0d1897c6d5b591626fb9b2b2032ccbbe22a8d1 | |
parent | 6f20dba6d25dac1a78558a07f4a7e0430546c03a (diff) |
PySide6/QSignalSpy: Add constructor taking a signal
Task-number: PYSIDE-1482
Change-Id: Ifb9061af1828e7348de3ad5407c830aeb7d4f386
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
(cherry picked from commit d3388316c3ac87c19cc668d9b5bca2b988db74ca)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
4 files changed, 76 insertions, 0 deletions
diff --git a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml index 1a7e7b8d8..bec575e54 100644 --- a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml +++ b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml @@ -51,6 +51,8 @@ <custom-type name="PyTypeObject"/> <custom-type name="PyUnicode"/> <custom-type name="list of QAbstractAnimation"/> + <custom-type name="PySideSignalInstance" + check-function="PySide::Signal::checkInstanceType"/> <!-- <function signature="qChecksum(QByteArrayView data, Qt::ChecksumType)"/> --> diff --git a/sources/pyside6/PySide6/QtTest/typesystem_test.xml b/sources/pyside6/PySide6/QtTest/typesystem_test.xml index 20dcf3672..ea480bbf2 100644 --- a/sources/pyside6/PySide6/QtTest/typesystem_test.xml +++ b/sources/pyside6/PySide6/QtTest/typesystem_test.xml @@ -107,9 +107,18 @@ </object-type> <object-type name="QSignalSpy"> <!-- Inherits QList<QVariantList> --> + <extra-includes> + <include file-name="pysidesignal.h" location="global"/> + </extra-includes> <declare-function signature="count()const" return-type="qsizetype"/> <declare-function signature="size()const" return-type="qsizetype"/> <declare-function signature="at(qsizetype)const" return-type="QVariantList"/> + <add-function signature="QSignalSpy(PySideSignalInstance@signal@)"> + <inject-code class="target" position="beginning" file="../glue/qttest.cpp" snippet="qsignalspy-signal"/> + <inject-documentation format="target" mode="append"> + Constructs a new QSignalSpy that listens for emissions of the signal. + </inject-documentation> + </add-function> </object-type> <namespace-type name="QTest"> diff --git a/sources/pyside6/PySide6/glue/qttest.cpp b/sources/pyside6/PySide6/glue/qttest.cpp new file mode 100644 index 000000000..4fea0a98c --- /dev/null +++ b/sources/pyside6/PySide6/glue/qttest.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt for Python. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/********************************************************************* + * INJECT CODE + ********************************************************************/ + +// @snippet qsignalspy-signal +auto *signalInst = reinterpret_cast<PySideSignalInstance *>(%PYARG_1); +PyObject *emitterPyObject = PySide::Signal::getObject(signalInst); +QObject* emitter = %CONVERTTOCPP[QObject *](emitterPyObject); +QByteArray signature = PySide::Signal::getSignature(signalInst); +if (!signature.isEmpty()) + signature.prepend('2'); // SIGNAL() macro + +if (emitter == nullptr || signature.isEmpty()) { + QByteArray error = QByteArrayLiteral("Wrong parameter (") + + (%PYARG_1)->ob_type->tp_name + + QByteArrayLiteral(") passed, QSignalSpy requires a signal."); + PyErr_SetString(PyExc_ValueError, error.constData()); + return -1; +} +%0 = new QSignalSpyWrapper(emitter, signature.constData()); +// @snippet qsignalspy-signal diff --git a/sources/pyside6/tests/QtTest/qsignalspy_test.py b/sources/pyside6/tests/QtTest/qsignalspy_test.py index 4e5786ed6..aa1b65bfa 100644 --- a/sources/pyside6/tests/QtTest/qsignalspy_test.py +++ b/sources/pyside6/tests/QtTest/qsignalspy_test.py @@ -57,6 +57,11 @@ class QSignalSpyTest(UsesQApplication): self._model.item(0, 0).setText('text2') self.assertEqual(spy.count(), 1) + def testSignal(self): + spy = QSignalSpy(self._model.dataChanged) + self._model.item(0, 0).setText('text3') + self.assertEqual(spy.count(), 1) + if __name__ == '__main__': unittest.main() |