aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside2/PySide2/glue/qtuitools.cpp
diff options
context:
space:
mode:
authorCristian Maureira-Fredes <cristian.maureira-fredes@qt.io>2018-11-29 10:29:34 +0100
committerCristian Maureira-Fredes <cristian.maureira-fredes@qt.io>2018-11-30 08:08:06 +0000
commit81e7fd946f172df3e567a6caef463b559505b106 (patch)
tree1690da23470e3652a0ce5dfd0f95fa0de48b6050 /sources/pyside2/PySide2/glue/qtuitools.cpp
parenteb84213e9cd16abd8f9eeed465e14c8f9181b7b6 (diff)
Move old glue code to snippets files
Most of the old glue code was directly injected into the typesystem, so it was possible to add them as snippets. There are still a couple of header files that will remain there, because the include tag does not have the file/snippet tags. A few lines of code were modified in favor of "modern" C++, and good practices. Task-number: PYSIDE-834 Change-Id: I3072298b16d7280550c6a7f6abae045250663ba6 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'sources/pyside2/PySide2/glue/qtuitools.cpp')
-rw-r--r--sources/pyside2/PySide2/glue/qtuitools.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/sources/pyside2/PySide2/glue/qtuitools.cpp b/sources/pyside2/PySide2/glue/qtuitools.cpp
index 0a2feb262..d0469e97d 100644
--- a/sources/pyside2/PySide2/glue/qtuitools.cpp
+++ b/sources/pyside2/PySide2/glue/qtuitools.cpp
@@ -36,6 +36,59 @@
** $QT_END_LICENSE$
**
****************************************************************************/
+// @snippet uitools-loadui
+/*
+ * Based on code provided by:
+ * Antonio Valentino <antonio.valentino at tiscali.it>
+ * Frédéric <frederic.mantegazza at gbiloba.org>
+ */
+
+#include <shiboken.h>
+#include <QUiLoader>
+#include <QFile>
+#include <QWidget>
+
+static void createChildrenNameAttributes(PyObject* root, QObject* object)
+{
+ for (auto *child : object->children()) {
+ const QByteArray name = child->objectName().toLocal8Bit();
+
+ if (!name.isEmpty() && !name.startsWith("_") && !name.startsWith("qt_")) {
+ if (!PyObject_HasAttrString(root, name.constData())) {
+ Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[QObject*](child));
+ PyObject_SetAttrString(root, name.constData(), pyChild);
+ }
+ createChildrenNameAttributes(root, child);
+ }
+ createChildrenNameAttributes(root, child);
+ }
+}
+
+static PyObject* QUiLoadedLoadUiFromDevice(QUiLoader* self, QIODevice* dev, QWidget* parent)
+{
+ QWidget* wdg = self->load(dev, parent);
+
+ if (wdg) {
+ PyObject* pyWdg = %CONVERTTOPYTHON[QWidget*](wdg);
+ createChildrenNameAttributes(pyWdg, wdg);
+ if (parent) {
+ Shiboken::AutoDecRef pyParent(%CONVERTTOPYTHON[QWidget*](parent));
+ Shiboken::Object::setParent(pyParent, pyWdg);
+ }
+ return pyWdg;
+ }
+
+ if (!PyErr_Occurred())
+ PyErr_SetString(PyExc_RuntimeError, "Unable to open/read ui device");
+ return nullptr;
+}
+
+static PyObject* QUiLoaderLoadUiFromFileName(QUiLoader* self, const QString& uiFile, QWidget* parent)
+{
+ QFile fd(uiFile);
+ return QUiLoadedLoadUiFromDevice(self, &fd, parent);
+}
+// @snippet uitools-loadui
// @snippet quiloader
Q_IMPORT_PLUGIN(PyCustomWidgets);