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/libpyside/signalmanager.cpp | |
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/libpyside/signalmanager.cpp')
-rw-r--r-- | sources/pyside6/libpyside/signalmanager.cpp | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/sources/pyside6/libpyside/signalmanager.cpp b/sources/pyside6/libpyside/signalmanager.cpp index efafb0aaf..753b50557 100644 --- a/sources/pyside6/libpyside/signalmanager.cpp +++ b/sources/pyside6/libpyside/signalmanager.cpp @@ -20,6 +20,7 @@ #include <sbkstring.h> #include <sbkstaticstrings.h> #include <sbkerrors.h> +#include <sbkenum_p.h> #include <QtCore/QByteArrayView> #include <QtCore/QDebug> @@ -129,6 +130,14 @@ PyObjectWrapper::operator PyObject *() const return m_me; } + +int PyObjectWrapper::toInt() const +{ + // hold the GIL + Shiboken::GilState state; + return Shiboken::Enum::check(m_me) ? Shiboken::Enum::getValue(m_me) : -1; +} + QDataStream &operator<<(QDataStream &out, const PyObjectWrapper &myObj) { if (Py_IsInitialized() == 0) { @@ -242,6 +251,8 @@ SignalManager::SignalManager() : m_d(new SignalManagerPrivate) // Register PyObject type to use in queued signal and slot connections qRegisterMetaType<PyObjectWrapper>("PyObject"); + // Register QVariant(enum) conversion to QVariant(int) + QMetaType::registerConverter<PyObjectWrapper, int>(&PyObjectWrapper::toInt); SbkConverter *converter = Shiboken::Conversions::createConverter(&PyBaseObject_Type, nullptr); Shiboken::Conversions::setCppPointerToPythonFunction(converter, PyObject_PTR_CppToPython_PyObject); |