From ad64c57acd08c47bcd2012396bdaa2c06d7ec083 Mon Sep 17 00:00:00 2001 From: Wanderer Fan Date: Sat, 7 May 2022 15:24:12 -0400 Subject: [PATCH] [Gui]add support for QGraphicsObject to PythonWrapper --- src/Gui/PythonWrapper.cpp | 24 +++++++++++++++++++++++- src/Gui/PythonWrapper.h | 10 ++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/Gui/PythonWrapper.cpp b/src/Gui/PythonWrapper.cpp index ec9fd2263e..84b26ead3a 100644 --- a/src/Gui/PythonWrapper.cpp +++ b/src/Gui/PythonWrapper.cpp @@ -26,6 +26,8 @@ # include # include # include +# include +# include #endif #include @@ -368,7 +370,7 @@ QObject* PythonWrapper::toQObject(const Py::Object& pyobject) QGraphicsItem* PythonWrapper::toQGraphicsItem(PyObject* pyPtr) { #if defined (HAVE_SHIBOKEN) && defined(HAVE_PYSIDE) - PyTypeObject* type = getPyTypeObjectForTypeName(); + PyTypeObject* type = getPyTypeObjectForTypeName(); if (type) { if (Shiboken::Object::checkType(pyPtr)) { SbkObject* sbkobject = reinterpret_cast(pyPtr); @@ -385,6 +387,26 @@ QGraphicsItem* PythonWrapper::toQGraphicsItem(PyObject* pyPtr) return nullptr; } +QGraphicsObject* PythonWrapper::toQGraphicsObject(PyObject* pyPtr) +{ +#if defined (HAVE_SHIBOKEN) && defined(HAVE_PYSIDE) + PyTypeObject* type = getPyTypeObjectForTypeName(); + if (type) { + if (Shiboken::Object::checkType(pyPtr)) { + SbkObject* sbkobject = reinterpret_cast(pyPtr); + void* cppobject = Shiboken::Object::cppPointer(sbkobject, type); + return reinterpret_cast(cppobject); + } + } +#else + // Access shiboken2/PySide2 via Python + // + void* ptr = qt_getCppPointer(Py::asObject(pyPtr), "shiboken2", "getCppPointer"); + return reinterpret_cast(ptr); +#endif + return nullptr; +} + Py::Object PythonWrapper::fromQIcon(const QIcon* icon) { #if defined (HAVE_SHIBOKEN) && defined(HAVE_PYSIDE) diff --git a/src/Gui/PythonWrapper.h b/src/Gui/PythonWrapper.h index 786de22eeb..10de58290d 100644 --- a/src/Gui/PythonWrapper.h +++ b/src/Gui/PythonWrapper.h @@ -24,13 +24,17 @@ #ifndef GUI_PYTHONWRAPPER_H #define GUI_PYTHONWRAPPER_H -#include +#include "qglobal.h" #include #include - QT_BEGIN_NAMESPACE class QDir; +class QIcon; +class QGraphicsObject; +class QGraphicsItem; +class QObject; +class QWidget; QT_END_NAMESPACE namespace Gui { @@ -47,6 +51,8 @@ public: bool toCString(const Py::Object&, std::string&); QObject* toQObject(const Py::Object&); QGraphicsItem* toQGraphicsItem(PyObject* ptr); + QGraphicsObject* toQGraphicsObject(PyObject* pyPtr); + Py::Object fromQObject(QObject*, const char* className=nullptr); Py::Object fromQWidget(QWidget*, const char* className=nullptr); const char* getWrapperName(QObject*) const;