Gui: Implement PythonWrapper::fromQAction

Wrapping QAction through QObject does not work as QAction class
is a part of QtGui not QtCore, so Py::Object with internal
pointer being null is returned causing a crash later.
Therefore implement fromQAction conversion method.
This commit is contained in:
Ladislav Michl
2024-02-24 02:21:30 +01:00
committed by wwmayer
parent fe70801fc2
commit 61bca92941
4 changed files with 28 additions and 3 deletions

View File

@@ -59,7 +59,7 @@ Py::Object CommandActionPy::getAction()
PythonWrapper wrap;
wrap.loadWidgetsModule();
return wrap.fromQObject(action->action());
return wrap.fromQAction(action->action());
}
else {
return Py::None();

View File

@@ -274,10 +274,10 @@ PyObject* CommandPy::getAction(PyObject *args)
if (group) {
const auto actions = group->actions();
for (auto a : actions)
list.append(wrap.fromQObject(a));
list.append(wrap.fromQAction(a));
}
else if (action) {
list.append(wrap.fromQObject(action->action()));
list.append(wrap.fromQAction(action->action()));
}
return Py::new_reference_to(list);

View File

@@ -25,6 +25,7 @@
# include <limits>
# include <unordered_map>
# include <list>
# include <QAction>
# include <QApplication>
# include <QDir>
# include <QIcon>
@@ -635,6 +636,28 @@ QDir* PythonWrapper::toQDir(PyObject* pyobj)
return qt_getCppType<QDir>(pyobj);
}
Py::Object PythonWrapper::fromQAction(QAction* action)
{
#if defined (HAVE_SHIBOKEN) && defined(HAVE_PYSIDE)
// Access shiboken/PySide via C++
auto type = getPyTypeObjectForTypeName<QAction>();
if (type) {
PyObject* pyobj = Shiboken::Object::newObject(type, action, false, false, "QAction");
WrapperManager::instance().addQObject(action, pyobj);
return Py::asObject(pyobj);
}
throw Py::RuntimeError("Failed to wrap action");
#else
// Access shiboken/PySide via Python
# if QT_VERSION < QT_VERSION_CHECK(6,0,0)
constexpr const char* qtModWithQAction = "QtWidgets";
# else
constexpr const char* qtModWithQAction = "QtGui";
# endif
return qt_wrapInstance<QAction*>(action, "QAction", qtModWithQAction);
#endif
}
Py::Object PythonWrapper::fromQPrinter(QPrinter* printer)
{
if (!printer) {

View File

@@ -29,6 +29,7 @@
#include <FCGlobal.h>
QT_BEGIN_NAMESPACE
class QAction;
class QDir;
class QIcon;
class QImage;
@@ -58,6 +59,7 @@ public:
QGraphicsObject* toQGraphicsObject(PyObject* pyPtr);
QGraphicsObject* toQGraphicsObject(const Py::Object& pyObject);
Py::Object fromQAction(QAction*);
Py::Object fromQPrinter(QPrinter*);
Py::Object fromQObject(QObject*, const char* className=nullptr);
Py::Object fromQWidget(QWidget*, const char* className=nullptr);