Skip to content

Commit

Permalink
Gui: Implement PythonWrapper::fromQAction
Browse files Browse the repository at this point in the history
QAction class resides in QtGui, so add its conversion method and
use it.
  • Loading branch information
3x380V committed Feb 24, 2024
1 parent 86500ca commit 323b0a6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Gui/CommandActionPy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Py::Object CommandActionPy::getAction()
PythonWrapper wrap;
wrap.loadWidgetsModule();

return wrap.fromQObject(action->action());
return wrap.fromQAction(action->action());
}
else {

Check warning on line 64 in src/Gui/CommandActionPy.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

do not use 'else' after 'return' [readability-else-after-return]
return Py::None();
Expand Down
4 changes: 2 additions & 2 deletions src/Gui/CommandPyImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions src/Gui/PythonWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,29 @@ QDir* PythonWrapper::toQDir(PyObject* pyobj)
return qt_getCppType<QDir>(pyobj);
}

Py::Object PythonWrapper::fromQAction(QAction* action)
{
if (!action) {
return Py::None();
}
#if defined (HAVE_SHIBOKEN) && defined(HAVE_PYSIDE)
// Access shiboken/PySide via C++
auto type = getPyTypeObjectForTypeName<QAction>();
if (type) {
std::string typeName = action->metaObject()->className();
PyObject* pyobj = Shiboken::Object::newObject(type, action, false, false, typeName.c_str());
WrapperManager::instance().addQObject(action, pyobj);
return Py::asObject(pyobj);
}
throw Py::RuntimeError("Failed to wrap object");
#else
// Access shiboken/PySide via Python

return qt_wrapInstance<QAction*>(action, action->metaObject()->className(), "QtGui");
#endif

}

Py::Object PythonWrapper::fromQPrinter(QPrinter* printer)
{
if (!printer) {
Expand Down
2 changes: 2 additions & 0 deletions src/Gui/PythonWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <FCGlobal.h>

QT_BEGIN_NAMESPACE
class QAction;
class QDir;
class QIcon;
class QImage;
Expand Down Expand Up @@ -58,6 +59,7 @@ class GuiExport PythonWrapper
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);
Expand Down

0 comments on commit 323b0a6

Please sign in to comment.