From 1f3315d020127ad5b9fb1aa4593acd3c9c9f8843 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Fri, 6 Jul 2018 20:14:39 +0200 Subject: [PATCH] Find MainWindow in Python Return Pointer from setupInterface() in Python Plugin Load plugins before creating MainWindow --- src/CutterApplication.cpp | 5 ++--- src/plugins/CutterPythonPlugin.cpp | 13 +++++++++++++ src/plugins/pluginSample.py | 11 +++++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/CutterApplication.cpp b/src/CutterApplication.cpp index 0801727a..1023105e 100644 --- a/src/CutterApplication.cpp +++ b/src/CutterApplication.cpp @@ -119,6 +119,8 @@ CutterApplication::CutterApplication(int &argc, char **argv) : QApplication(argc } } + loadPlugins(); + mainWindow = new MainWindow(); installEventFilter(mainWindow); @@ -161,9 +163,6 @@ CutterApplication::CutterApplication(int &argc, char **argv) : QApplication(argc mainWindow->openNewFile(options, analLevelSpecified); } - // Load plugins - loadPlugins(); - #ifdef CUTTER_APPVEYOR_R2DEC qputenv("R2DEC_HOME", "radare2\\lib\\plugins\\r2dec-js"); #endif diff --git a/src/plugins/CutterPythonPlugin.cpp b/src/plugins/CutterPythonPlugin.cpp index 9764edcc..a85058be 100644 --- a/src/plugins/CutterPythonPlugin.cpp +++ b/src/plugins/CutterPythonPlugin.cpp @@ -80,10 +80,23 @@ CutterDockWidget* CutterPythonPlugin::setupInterface(MainWindow *main, QAction * PyObject *pWidget = nullptr; Python()->restoreThread(); pWidget = PyObject_CallMethod(pInstance, "setupInterface", nullptr); + if (!pWidget) { qWarning() << "Error in setupInterface()."; PyErr_Print(); + Python()->saveThread(); + return nullptr; } + + if (!PyLong_Check(pWidget)) { + qWarning() << "Value returned by setupInterface() is not PyLong."; + Python()->saveThread(); + return nullptr; + } + + auto dockWidget = reinterpret_cast(PyLong_AsLong(pWidget)); + printf("plugin gave me this: %s\n", dockWidget->objectName().toLocal8Bit().constData()); + Python()->saveThread(); return nullptr; diff --git a/src/plugins/pluginSample.py b/src/plugins/pluginSample.py index a57f3318..23d3c3e0 100644 --- a/src/plugins/pluginSample.py +++ b/src/plugins/pluginSample.py @@ -1,6 +1,7 @@ import cutter from cutter_plugin import CutterPlugin from PySide2 import QtCore, QtWidgets +import shiboken2 class CutterSamplePlugin(CutterPlugin): def setupPlugin(self): @@ -12,11 +13,17 @@ class CutterSamplePlugin(CutterPlugin): def setupInterface(self): print('Creating the dock widget...') - main_window = self.app.findChild('MainWindow') + main_window = None + for widget in QtWidgets.QApplication.topLevelWidgets(): + if widget.objectName() == "MainWindow": + main_window = widget dock_widget = QtWidgets.QDockWidget(main_window) + dock_widget.setObjectName("FancyDockWidgetFromCoolPlugin") dock_widget.setWindowTitle('Test Widget') print(main_window, dock_widget) - return dock_widget + ptr = shiboken2.getCppPointer(dock_widget)[0] + print(ptr) + return ptr # Instantiate our plugin