aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/quick/items/qquickview.cpp54
-rw-r--r--src/quick/items/qquickview_p.h2
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);