aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/tests/pysidetest/testqvariantenum.cpp
diff options
context:
space:
mode:
authorShyamnath Premnadh <Shyamnath.Premnadh@qt.io>2023-05-30 17:20:34 +0200
committerShyamnath Premnadh <Shyamnath.Premnadh@qt.io>2023-06-13 18:07:15 +0200
commit59581e630740a5216c57a6b9ee6c3742f10e4323 (patch)
tree399b7af756df5616756acb0a2d1c80a1666e3ffc /sources/pyside6/tests/pysidetest/testqvariantenum.cpp
parentbc311d1eca86ea237d8822fe22837db7c3f46ee9 (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/pysidetest/testqvariantenum.cpp')
-rw-r--r--sources/pyside6/tests/pysidetest/testqvariantenum.cpp29
1 files changed, 29 insertions, 0 deletions
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());
+}