diff options
Diffstat (limited to 'sources/pyside2/plugins/customwidget.cpp')
-rw-r--r-- | sources/pyside2/plugins/customwidget.cpp | 66 |
1 files changed, 28 insertions, 38 deletions
diff --git a/sources/pyside2/plugins/customwidget.cpp b/sources/pyside2/plugins/customwidget.cpp index 6a6d7a3be..3c54b02e4 100644 --- a/sources/pyside2/plugins/customwidget.cpp +++ b/sources/pyside2/plugins/customwidget.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2020 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of Qt for Python. @@ -37,26 +37,15 @@ ** ****************************************************************************/ - #include "customwidget.h" +#include <QtCore/qdebug.h> - -struct PyCustomWidgetPrivate -{ - PyObject *pyObject; - bool initialized; -}; - -PyCustomWidget::PyCustomWidget(PyObject *objectType) - : m_data(new PyCustomWidgetPrivate()) +// Part of the static plugin linked to the QtUiLoader Python module, +// allowing it to create a custom widget written in Python. +PyCustomWidget::PyCustomWidget(PyObject *objectType) : + m_pyObject(objectType), + m_name(QString::fromUtf8(reinterpret_cast<PyTypeObject *>(objectType)->tp_name)) { - m_data->pyObject = objectType; - m_name = QString(reinterpret_cast<PyTypeObject *>(objectType)->tp_name); -} - -PyCustomWidget::~PyCustomWidget() -{ - delete m_data; } bool PyCustomWidget::isContainer() const @@ -66,7 +55,7 @@ bool PyCustomWidget::isContainer() const bool PyCustomWidget::isInitialized() const { - return m_data->initialized; + return m_initialized; } QIcon PyCustomWidget::icon() const @@ -106,9 +95,9 @@ QString PyCustomWidget::whatsThis() const QWidget *PyCustomWidget::createWidget(QWidget *parent) { - //Create a python instance and return cpp object - PyObject *pyParent; - bool unkowParent = false; + // Create a python instance and return cpp object + PyObject *pyParent = nullptr; + bool unknownParent = false; if (parent) { pyParent = reinterpret_cast<PyObject *>(Shiboken::BindingManager::instance().retrieveWrapper(parent)); if (pyParent) { @@ -116,7 +105,7 @@ QWidget *PyCustomWidget::createWidget(QWidget *parent) } else { static Shiboken::Conversions::SpecificConverter converter("QWidget*"); pyParent = converter.toPython(&parent); - unkowParent = true; + unknownParent = true; } } else { Py_INCREF(Py_None); @@ -124,25 +113,26 @@ QWidget *PyCustomWidget::createWidget(QWidget *parent) } Shiboken::AutoDecRef pyArgs(PyTuple_New(1)); - PyTuple_SET_ITEM(pyArgs, 0, pyParent); //tuple will keep pyParent reference - - //Call python constructor - auto result = reinterpret_cast<SbkObject *>(PyObject_CallObject(m_data->pyObject, pyArgs)); - - QWidget *widget = nullptr; - if (result) { - if (unkowParent) //if parent does not exists in python, transfer the ownership to cpp - Shiboken::Object::releaseOwnership(result); - else - Shiboken::Object::setParent(pyParent, reinterpret_cast<PyObject *>(result)); - - widget = reinterpret_cast<QWidget *>(Shiboken::Object::cppPointer(result, Py_TYPE(result))); + PyTuple_SET_ITEM(pyArgs, 0, pyParent); // tuple will keep pyParent reference + + // Call python constructor + auto result = reinterpret_cast<SbkObject *>(PyObject_CallObject(m_pyObject, pyArgs)); + if (!result) { + qWarning("Unable to create a Python custom widget of type \"%s\".", + qPrintable(m_name)); + PyErr_Print(); + return nullptr; } - return widget; + if (unknownParent) // if parent does not exist in python, transfer the ownership to cpp + Shiboken::Object::releaseOwnership(result); + else + Shiboken::Object::setParent(pyParent, reinterpret_cast<PyObject *>(result)); + + return reinterpret_cast<QWidget *>(Shiboken::Object::cppPointer(result, Py_TYPE(result))); } void PyCustomWidget::initialize(QDesignerFormEditorInterface *core) { - m_data->initialized = true; + m_initialized = true; } |