From b202eb2f6b1b6cf244d5aeb29f71dc5baa1b0bf2 Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 3 Aug 2023 00:04:45 +0200 Subject: [PATCH] Gui: support of wrapping QImage --- src/Gui/PythonWrapper.cpp | 32 +++++++++++++++++++++++++++++++- src/Gui/PythonWrapper.h | 4 ++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Gui/PythonWrapper.cpp b/src/Gui/PythonWrapper.cpp index 5339c1f64c..702b18c0a8 100644 --- a/src/Gui/PythonWrapper.cpp +++ b/src/Gui/PythonWrapper.cpp @@ -583,9 +583,39 @@ QGraphicsObject* PythonWrapper::toQGraphicsObject(PyObject* pyPtr) QGraphicsObject* PythonWrapper::toQGraphicsObject(const Py::Object& pyobject) { -return toQGraphicsObject(pyobject.ptr()); + return toQGraphicsObject(pyobject.ptr()); } +Py::Object PythonWrapper::fromQImage(const QImage& img) +{ +#if defined (HAVE_SHIBOKEN) && defined(HAVE_PYSIDE) +#if defined (HAVE_SHIBOKEN2) + PyObject* pyobj = Shiboken::Conversions::copyToPython(reinterpret_cast(getPyTypeObjectForTypeName()), + const_cast(&img)); +#else + PyObject* pyobj = Shiboken::Conversions::copyToPython(getPyTypeObjectForTypeName(), + const_cast(&img)); +#endif + if (pyobj) { + return Py::asObject(pyobj); + } +#else + // Access shiboken/PySide via Python + // + return qt_wrapInstance(&img, "QImage", shiboken, PySide + ".QtGui", "wrapInstance"); +#endif + throw Py::RuntimeError("Failed to wrap icon"); +} + +QImage *PythonWrapper::toQImage(PyObject *pyobj) +{ +#if defined (HAVE_SHIBOKEN) && defined(HAVE_PYSIDE) + return qt_getCppType(pyobj); +#else + Q_UNUSED(pyobj); +#endif + return nullptr; +} Py::Object PythonWrapper::fromQIcon(const QIcon* icon) { diff --git a/src/Gui/PythonWrapper.h b/src/Gui/PythonWrapper.h index c31f30e471..8326d69e39 100644 --- a/src/Gui/PythonWrapper.h +++ b/src/Gui/PythonWrapper.h @@ -31,6 +31,7 @@ QT_BEGIN_NAMESPACE class QDir; class QIcon; +class QImage; class QGraphicsObject; class QGraphicsItem; class QObject; @@ -61,6 +62,9 @@ public: Py::Object fromQObject(QObject*, const char* className=nullptr); Py::Object fromQWidget(QWidget*, const char* className=nullptr); const char* getWrapperName(QObject*) const; + + Py::Object fromQImage(const QImage&); + QImage *toQImage(PyObject *pyobj); /*! Create a Python wrapper for the icon. The icon must be created on the heap and the Python wrapper takes ownership of it.