diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-05-20 14:37:12 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2020-05-21 00:22:19 +0000 |
commit | e7dbcc3f00b396ec57926e8cb899b2a5735dcd05 (patch) | |
tree | 1ea99940fa933ba2929fe8cd235bfc58bf465ba4 /src/quick | |
parent | 74ce060778dbcd7401049d170db54cf64eb27cd2 (diff) |
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
Change-Id: I79ed37d22d304bcc6d4e3c956b83a65fe157dfe0
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
(cherry picked from commit a64ee3a2481499f856d0f3fbc697399e0df1e8f8)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src/quick')
-rw-r--r-- | src/quick/items/qquickview.cpp | 54 | ||||
-rw-r--r-- | src/quick/items/qquickview_p.h | 2 |
2 files changed, 33 insertions, 23 deletions
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<QQuickItem *>(obj)) { root = sgItem; sgItem->setParentItem(q->QQuickWindow::contentItem()); QQml_setParent_noEvent(sgItem, q->QQuickWindow::contentItem()); - } else if (qobject_cast<QWindow *>(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<QWindow *>(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; } /*! diff --git a/src/quick/items/qquickview_p.h b/src/quick/items/qquickview_p.h index b1ab8d8e8c..1bc266bbbf 100644 --- a/src/quick/items/qquickview_p.h +++ b/src/quick/items/qquickview_p.h @@ -91,7 +91,7 @@ public: void itemGeometryChanged(QQuickItem *item, QQuickGeometryChange change, const QRectF &) override; void initResize(); void updateSize(); - void setRootObject(QObject *); + bool setRootObject(QObject *); void init(QQmlEngine* e = nullptr); |