diff options
| author | Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> | 2023-05-30 17:20:34 +0200 |
|---|---|---|
| committer | Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> | 2023-06-13 18:07:15 +0200 |
| commit | 59581e630740a5216c57a6b9ee6c3742f10e4323 (patch) | |
| tree | 399b7af756df5616756acb0a2d1c80a1666e3ffc /sources/pyside6/tests | |
| parent | bc311d1eca86ea237d8822fe22837db7c3f46ee9 (diff) | |
Enum: Enable toInt for QVariant(PyEnum/SbkEnum)
- For Python/Shiboken types not known to Qt that requires wrapping
around a QVariant, we use the PyObjectWrapper type. This patch
registers a toInt() QMetaType converter for PyObjectWrapper, which
enables automatic conversion to int for a QVariant(PyObjectWrapper)
within C++ i.e. QVariant(PyObjectWrapper).toInt() will work
- This means that cases like QAbstractItemModel::data() that calls
QtPrivate::legacyEnumValueFromModelData(const QVariant &data) would
work without explicit conversion from QVariant(PyObjectWrapper) to
QVariant(int). But for cases like QMetaProperty::write() explcit
handling is still required.
- This would also work for cases where the QVariant(PyObjectWrapper) is
simply channeled from Python to C++, and from C++ back to Python
without performing any operations on it.
- Incase, the wrapped object is not a PyEnum/ShibokenEnum object, then
toInt() would return a -1.
Pick-to: 6.5
Task-number: PYSIDE-1930
Task-number: PYSIDE-2339
Change-Id: I983351f2ff88c79c29399c257e38421116efc7a3
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'sources/pyside6/tests')
6 files changed, 91 insertions, 1 deletions
diff --git a/sources/pyside6/tests/pysidetest/CMakeLists.txt b/sources/pyside6/tests/pysidetest/CMakeLists.txt index f64cea904..6dc56735d 100644 --- a/sources/pyside6/tests/pysidetest/CMakeLists.txt +++ b/sources/pyside6/tests/pysidetest/CMakeLists.txt @@ -28,6 +28,7 @@ pysidetest_macros.h sharedpointertestbench.cpp sharedpointertestbench.h testobject.cpp testobject.h testview.cpp testview.h +testqvariantenum.cpp testqvariantenum.h ) set(testbinding_SRC @@ -44,6 +45,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/testbinding/qsharedpointer_int_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/testbinding/sharedpointertestbench_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/testbinding/testview_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/testbinding/testbinding_module_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/testbinding/testqvariantenum_wrapper.cpp ) # Get per module include dirs. diff --git a/sources/pyside6/tests/pysidetest/pysidetest_global.h b/sources/pyside6/tests/pysidetest/pysidetest_global.h index 461b6f56f..6f784dc58 100644 --- a/sources/pyside6/tests/pysidetest/pysidetest_global.h +++ b/sources/pyside6/tests/pysidetest/pysidetest_global.h @@ -11,5 +11,6 @@ #include "flagstest.h" #include "hiddenobject.h" #include "sharedpointertestbench.h" +#include "testqvariantenum.h" #endif // PYSIDETEST_GLOBAL_H diff --git a/sources/pyside6/tests/pysidetest/qvariant_test.py b/sources/pyside6/tests/pysidetest/qvariant_test.py index 5addb00e0..df623146d 100644 --- a/sources/pyside6/tests/pysidetest/qvariant_test.py +++ b/sources/pyside6/tests/pysidetest/qvariant_test.py @@ -1,6 +1,7 @@ # Copyright (C) 2022 The Qt Company Ltd. # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +import enum import os import sys import unittest @@ -10,13 +11,25 @@ sys.path.append(os.fspath(Path(__file__).resolve().parents[1])) from init_paths import init_test_paths init_test_paths(True) -from testbinding import TestObject +from testbinding import TestObject, TestQVariantEnum from PySide6.QtCore import Qt, QKeyCombination from PySide6.QtGui import QKeySequence, QAction from helper.usesqapplication import UsesQApplication +class PyTestQVariantEnum(TestQVariantEnum): + def __init__(self, var_enum): + super().__init__(var_enum) + + def getRValEnum(self): + return Qt.Orientation.Vertical + + def channelingEnum(self, rval_enum): + return (isinstance(rval_enum, enum.Enum) and + rval_enum == Qt.Orientation.Vertical) + + class QVariantTest(UsesQApplication): def testQKeySequenceQVariantOperator(self): @@ -35,6 +48,24 @@ class QVariantTest(UsesQApplication): # Issues a warning but works as well QKeySequence(Qt.CTRL + Qt.Key_Q) + def testEnum(self): + # Testing C++ class + testqvariant = TestQVariantEnum(Qt.CheckState.Checked) + self.assertEqual(testqvariant.getLValEnum(), Qt.CheckState.Checked) + self.assertIsInstance(testqvariant.getLValEnum(), enum.Enum) + # in the case where we return a QVariant of C++ enum, it returns a + # QVariant(int) to Python unless explicitly handled manually by Shiboken + self.assertEqual(testqvariant.getRValEnum(), 1) + self.assertEqual(testqvariant.isEnumChanneled(), False) + + # Testing Python child class + pytestqvariant = PyTestQVariantEnum(Qt.CheckState.Checked) + self.assertEqual(pytestqvariant.isEnumChanneled(), True) + # check toInt() conversion works for PyObjectWrapper + self.assertEqual(PyTestQVariantEnum.getNumberFromQVarEnum(Qt.Orientation.Vertical), 2) + # check toInt() conversion for IntEnum + self.assertEqual(PyTestQVariantEnum.getNumberFromQVarEnum(Qt.GestureType.TapGesture), 1) + if __name__ == '__main__': unittest.main() diff --git a/sources/pyside6/tests/pysidetest/testqvariantenum.cpp b/sources/pyside6/tests/pysidetest/testqvariantenum.cpp new file mode 100644 index 000000000..7135e422a --- /dev/null +++ b/sources/pyside6/tests/pysidetest/testqvariantenum.cpp @@ -0,0 +1,29 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#include "testqvariantenum.h" + +QVariant TestQVariantEnum::getLValEnum() const +{ + return this->m_enum; +} + +QVariant TestQVariantEnum::getRValEnum() const +{ + return QVariant(Qt::Orientation::Horizontal); +} + +int TestQVariantEnum::getNumberFromQVarEnum(QVariant variantEnum) +{ + return variantEnum.toInt(); +} + +bool TestQVariantEnum::channelingEnum([[maybe_unused]] QVariant rvalEnum) const +{ + return false; +} + +bool TestQVariantEnum::isEnumChanneled() const +{ + return this->channelingEnum(this->getRValEnum()); +} diff --git a/sources/pyside6/tests/pysidetest/testqvariantenum.h b/sources/pyside6/tests/pysidetest/testqvariantenum.h new file mode 100644 index 000000000..46564d268 --- /dev/null +++ b/sources/pyside6/tests/pysidetest/testqvariantenum.h @@ -0,0 +1,25 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#ifndef TESTQVARIANT_H +#define TESTQVARIANT_H + +#include "pysidetest_macros.h" + +#include <QtCore/QVariant> + +class PYSIDETEST_API TestQVariantEnum +{ +public: + TestQVariantEnum(QVariant lvalue_enum) : m_enum(lvalue_enum) {} + QVariant getLValEnum() const; + static int getNumberFromQVarEnum(QVariant variantEnum = QVariant()); + bool isEnumChanneled() const; + virtual QVariant getRValEnum() const; + virtual bool channelingEnum(QVariant rvalEnum) const; + virtual ~TestQVariantEnum() = default; +private: + QVariant m_enum; +}; + +#endif // TESTQVARIANT_H diff --git a/sources/pyside6/tests/pysidetest/typesystem_pysidetest.xml b/sources/pyside6/tests/pysidetest/typesystem_pysidetest.xml index 7f1170466..f8a8d2b6b 100644 --- a/sources/pyside6/tests/pysidetest/typesystem_pysidetest.xml +++ b/sources/pyside6/tests/pysidetest/typesystem_pysidetest.xml @@ -63,6 +63,8 @@ </modify-function> </object-type> + <value-type name="TestQVariantEnum"/> + <namespace-type name="FlagsNamespace" visible="no"> <enum-type name="Option" flags="Options"/> <object-type name="ClassForEnum" /> |
