From a64ee3a2481499f856d0f3fbc697399e0df1e8f8 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Wed, 20 May 2020 14:37:12 +0200 Subject: Fix handling of QQuickViewPrivate::root QQuickView is supposed to own the root item and shall also track it for external deletion. Therefore, when we assign a new root item we need to delete the old one. There is no point in setting a QPointer to nullptr after deleting it. It will do that by itself. When we fail to assign a new item, we should _not_ automatically delete the new one. Calling code typically does not expect the argument to a set* call to be deleted right away. Rather, return a boolean indicating whether we have successfully set the root object. This can then be used to get rid of the object if necessary. Coverity-Id: 218729 Pick-to: 5.15 Pick-to: 5.12 Change-Id: I79ed37d22d304bcc6d4e3c956b83a65fe157dfe0 Reviewed-by: Shawn Rutledge --- src/quick/items/qquickview.cpp | 54 +++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 22 deletions(-) (limited to 'src/quick/items/qquickview.cpp') diff --git a/src/quick/items/qquickview.cpp b/src/quick/items/qquickview.cpp index 5b3b954460..97f6689d8a 100644 --- a/src/quick/items/qquickview.cpp +++ b/src/quick/items/qquickview.cpp @@ -74,7 +74,7 @@ void QQuickViewPrivate::init(QQmlEngine* e) } QQuickViewPrivate::QQuickViewPrivate() - : root(nullptr), component(nullptr), resizeMode(QQuickView::SizeViewToRootObject), initialSize(0,0) + : component(nullptr), resizeMode(QQuickView::SizeViewToRootObject), initialSize(0,0) { } @@ -90,10 +90,8 @@ void QQuickViewPrivate::execute() return; } - if (root) { + if (root) delete root; - root = nullptr; - } if (component) { delete component; component = nullptr; @@ -209,7 +207,6 @@ QQuickView::~QQuickView() // be a child of the QQuickViewPrivate, and will be destroyed by its dtor Q_D(QQuickView); delete d->root; - d->root = nullptr; } /*! @@ -275,7 +272,8 @@ void QQuickView::setContent(const QUrl& url, QQmlComponent *component, QObject* return; } - d->setRootObject(item); + if (!d->setRootObject(item)) + delete item; emit statusChanged(status()); } @@ -500,43 +498,55 @@ void QQuickView::continueExecute() return; } - d->setRootObject(obj.take()); + if (d->setRootObject(obj.get())) + obj.take(); emit statusChanged(status()); } /*! \internal + + Sets \a obj as root object and returns true if that operation succeeds. + Otherwise returns \c false. If \c false is returned, the root object is + \c nullptr afterwards. You can explicitly set the root object to nullptr, + and the return value will be \c true. */ -void QQuickViewPrivate::setRootObject(QObject *obj) +bool QQuickViewPrivate::setRootObject(QObject *obj) { Q_Q(QQuickView); if (root == obj) - return; + return true; + + delete root; + if (obj == nullptr) + return true; + if (QQuickItem *sgItem = qobject_cast(obj)) { root = sgItem; sgItem->setParentItem(q->QQuickWindow::contentItem()); QQml_setParent_noEvent(sgItem, q->QQuickWindow::contentItem()); - } else if (qobject_cast(obj)) { - qWarning() << "QQuickView does not support using windows as a root item." << Qt::endl - << Qt::endl - << "If you wish to create your root window from QML, consider using QQmlApplicationEngine instead." << Qt::endl; - } else { - qWarning() << "QQuickView only supports loading of root objects that derive from QQuickItem." << Qt::endl - << Qt::endl - << "Ensure your QML code is written for QtQuick 2, and uses a root that is or" << Qt::endl - << "inherits from QtQuick's Item (not a Timer, QtObject, etc)." << Qt::endl; - delete obj; - root = nullptr; - } - if (root) { initialSize = rootObjectSize(); if ((resizeMode == QQuickView::SizeViewToRootObject || q->width() <= 1 || q->height() <= 1) && initialSize != q->size()) { q->resize(initialSize); } initResize(); + return true; + } + + if (qobject_cast(obj)) { + qWarning() << "QQuickView does not support using a window as a root item." << Qt::endl + << Qt::endl + << "If you wish to create your root window from QML, consider using QQmlApplicationEngine instead." << Qt::endl; + return false; } + + qWarning() << "QQuickView only supports loading of root objects that derive from QQuickItem." << Qt::endl + << Qt::endl + << "Ensure your QML code is written for QtQuick 2, and uses a root that is or" << Qt::endl + << "inherits from QtQuick's Item (not a Timer, QtObject, etc)." << Qt::endl; + return false; } /*! -- cgit v1.2.3