aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/items/qquickview.cpp
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2020-05-20 14:37:12 +0200
committerUlf Hermann <ulf.hermann@qt.io>2020-05-20 22:43:25 +0200
commita64ee3a2481499f856d0f3fbc697399e0df1e8f8 (patch)
treebd4994eb797afe3f3dca66670cf72bf0da89d590 /src/quick/items/qquickview.cpp
parent542fdc075ebc7853869b285d6ac8fd75bfd2aafd (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 Pick-to: 5.15 Pick-to: 5.12 Change-Id: I79ed37d22d304bcc6d4e3c956b83a65fe157dfe0 Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src/quick/items/qquickview.cpp')
-rw-r--r--src/quick/items/qquickview.cpp54
1 files changed, 32 insertions, 22 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;
}
/*!