QQuickView/QQuickWidget: Report an error if rootObject ends up null.
authorRobin Burchell <robin.burchell@viroteck.net>
Mon, 2 Feb 2015 21:17:41 +0000 (22:17 +0100)
committerRobin Burchell <robin.burchell@viroteck.net>
Mon, 23 Feb 2015 12:43:06 +0000 (12:43 +0000)
This can happen when trying to use a non-QQuickItem item as the root item in a
QQuickView, for instance, a Window or ApplicationWindow item.

This generates a warning (correctly), but does not set an error state on the
view, so automated tooling and the like does not know that the scene was not
successfully loaded.

Change-Id: I1dc4191ef07187e9b1929995aedb01c155b0957c
Reviewed-by: Alan Alpert <aalpert@blackberry.com>
src/quick/items/qquickview.cpp
src/quickwidgets/qquickwidget.cpp
tests/auto/quick/qquickview/data/error2.qml [new file with mode: 0644]
tests/auto/quick/qquickview/tst_qquickview.cpp

index 5b5413a..a463f0a 100644 (file)
@@ -345,6 +345,9 @@ QQuickView::Status QQuickView::status() const
     if (!d->component)
         return QQuickView::Null;
 
+    if (d->component->status() == QQmlComponent::Ready && !d->root)
+        return QQuickView::Error;
+
     return QQuickView::Status(d->component->status());
 }
 
@@ -364,6 +367,10 @@ QList<QQmlError> QQuickView::errors() const
         QQmlError error;
         error.setDescription(QLatin1String("QQuickView: invalid qml engine."));
         errs << error;
+    } else if (d->component->status() == QQmlComponent::Ready && !d->root) {
+        QQmlError error;
+        error.setDescription(QLatin1String("QQuickView: invalid root object."));
+        errs << error;
     }
 
     return errs;
index 3bdf0bf..f5b2122 100644 (file)
@@ -530,6 +530,9 @@ QQuickWidget::Status QQuickWidget::status() const
     if (!d->component)
         return QQuickWidget::Null;
 
+    if (d->component->status() == QQmlComponent::Ready && !d->root)
+        return QQuickWidget::Error;
+
     return QQuickWidget::Status(d->component->status());
 }
 
@@ -551,6 +554,10 @@ QList<QQmlError> QQuickWidget::errors() const
         QQmlError error;
         error.setDescription(QLatin1String("QQuickWidget: invalid qml engine."));
         errs << error;
+    } else if (d->component->status() == QQmlComponent::Ready && !d->root) {
+        QQmlError error;
+        error.setDescription(QLatin1String("QQuickWidget: invalid root object."));
+        errs << error;
     }
 
     return errs;
diff --git a/tests/auto/quick/qquickview/data/error2.qml b/tests/auto/quick/qquickview/data/error2.qml
new file mode 100644 (file)
index 0000000..1d754b6
--- /dev/null
@@ -0,0 +1,4 @@
+import QtQuick.Window 2.0
+
+Window {
+}
index a980c69..7ee24bf 100644 (file)
@@ -187,6 +187,15 @@ void tst_QQuickView::errors()
         QVERIFY(view.status() == QQuickView::Error);
         QVERIFY(view.errors().count() == 1);
     }
+
+    {
+        QQuickView view;
+        QQmlTestMessageHandler messageHandler;
+        view.setSource(testFileUrl("error2.qml"));
+        QVERIFY(view.status() == QQuickView::Error);
+        QVERIFY(view.errors().count() == 1);
+        view.show();
+    }
 }
 
 void tst_QQuickView::engine()