From 0b6dd91fbd02e4de1f653e2a8cd6e892628dac0c Mon Sep 17 00:00:00 2001 From: Christian Tismer Date: Wed, 13 Nov 2019 19:12:16 +0100 Subject: qApp: Ensure QtCore import when embedded QApplication subclass is used The qApp machinery works great with Python. When using embedding, things are different because there is no longer a wrapper layer. Unfortunately, many extension modules use C++ to derive a QApplication class. This has the side effect that when a foreign C++ module gets imported, the qApp machinery does not see it as it would in Python. Instead of a complex analysis, we always make sure that QtCore is imported. It will report the right instance, anyway. This change could not easily be tested. It was confirmed as a solution by Antonio Rojas. Change-Id: Ie9c56ac75e6c0ae3ace615dfc26c6d218ff4efea Fixes: PYSIDE-1135 Reviewed-by: Friedemann Kleint --- sources/shiboken2/libshiboken/qapp_macro.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sources/shiboken2/libshiboken/qapp_macro.cpp b/sources/shiboken2/libshiboken/qapp_macro.cpp index 306f53b74..c2018bd6f 100644 --- a/sources/shiboken2/libshiboken/qapp_macro.cpp +++ b/sources/shiboken2/libshiboken/qapp_macro.cpp @@ -246,7 +246,14 @@ NotifyModuleForQApp(PyObject *module, void *qApp) * qApp_contents variable and assigns the instance, instead of vice-versa. */ PyObject *coreDict = qApp_moduledicts[1]; - if (qApp != nullptr && coreDict != nullptr) { + if (coreDict == nullptr) { + // PYSIDE-1135: Make sure that at least QtCore gets imported. + // That problem exists when a derived instance is created in C++. + qApp_moduledicts[1] = Py_None; // anything != nullptr during import + coreDict = PyImport_ImportModule("PySide2.QtCore"); + qApp_moduledicts[1] = coreDict; + } + if (qApp != nullptr && coreDict != nullptr && coreDict != Py_None) { PyObject *coreApp = PyDict_GetItemString(coreDict, "QCoreApplication"); if (coreApp != nullptr) { qApp_content = PyObject_CallMethod(coreApp, "instance", ""); -- cgit v1.2.3