diff --git a/src/declarative/items/qsgcanvas.cpp b/src/declarative/items/qsgcanvas.cpp
index 6214da65c75e1335fa0b6941f7629e4da349b8e8..d17df05ee38168e949e1f59e17c48320b6425585 100644
--- a/src/declarative/items/qsgcanvas.cpp
+++ b/src/declarative/items/qsgcanvas.cpp
@@ -260,6 +260,19 @@ void QSGCanvas::hideEvent(QHideEvent *e)
     QGLWidget::hideEvent(e);
 }
 
+void QSGCanvas::focusOutEvent(QFocusEvent *event)
+{
+    Q_D(QSGCanvas);
+    d->rootItem->setFocus(false);
+    QGLWidget::focusOutEvent(event);
+}
+
+void QSGCanvas::focusInEvent(QFocusEvent *event)
+{
+    Q_D(QSGCanvas);
+    d->rootItem->setFocus(true);
+    QGLWidget::focusInEvent(event);
+}
 
 void QSGCanvasPrivate::initializeSceneGraph()
 {
@@ -480,9 +493,6 @@ void QSGCanvasPrivate::init(QSGCanvas *c)
     QSGItemPrivate *rootItemPrivate = QSGItemPrivate::get(rootItem);
     rootItemPrivate->canvas = q;
     rootItemPrivate->flags |= QSGItem::ItemIsFocusScope;
-    rootItemPrivate->focus = true;
-    rootItemPrivate->activeFocus = true;
-    activeFocusItem = rootItem;
 
     context = QSGContext::createDefaultContext();
 }
@@ -633,17 +643,18 @@ void QSGCanvasPrivate::setFocusInScope(QSGItem *scope, QSGItem *item, FocusOptio
     Q_Q(QSGCanvas);
 
     Q_ASSERT(item);
-    Q_ASSERT(scope);
+    Q_ASSERT(scope || item == rootItem);
 
 #ifdef FOCUS_DEBUG
     qWarning() << "QSGCanvasPrivate::setFocusInScope():";
     qWarning() << "    scope:" << (QObject *)scope;
-    qWarning() << "    scopeSubFocusItem:" << (QObject *)QSGItemPrivate::get(scope)->subFocusItem;
+    if (scope)
+        qWarning() << "    scopeSubFocusItem:" << (QObject *)QSGItemPrivate::get(scope)->subFocusItem;
     qWarning() << "    item:" << (QObject *)item;
     qWarning() << "    activeFocusItem:" << (QObject *)activeFocusItem;
 #endif
 
-    QSGItemPrivate *scopePrivate = QSGItemPrivate::get(scope);
+    QSGItemPrivate *scopePrivate = scope ? QSGItemPrivate::get(scope) : 0;
     QSGItemPrivate *itemPrivate = QSGItemPrivate::get(item);
 
     QSGItem *oldActiveFocusItem = 0;
@@ -652,69 +663,73 @@ void QSGCanvasPrivate::setFocusInScope(QSGItem *scope, QSGItem *item, FocusOptio
     QVarLengthArray<QSGItem *, 20> changed;
 
     // Does this change the active focus?
-    if (scopePrivate->activeFocus) {
+    if (item == rootItem || scopePrivate->activeFocus) {
         oldActiveFocusItem = activeFocusItem;
         newActiveFocusItem = item;
         while (newActiveFocusItem->isFocusScope() && newActiveFocusItem->scopedFocusItem())
             newActiveFocusItem = newActiveFocusItem->scopedFocusItem();
 
-        Q_ASSERT(oldActiveFocusItem);
-
+        if (oldActiveFocusItem) {
 #ifndef QT_NO_IM
-        if (QInputContext *ic = inputContext())
-            ic->reset();
+            if (QInputContext *ic = inputContext())
+                ic->reset();
 #endif
 
-        activeFocusItem = 0;
-        QFocusEvent event(QEvent::FocusOut, Qt::OtherFocusReason);
-        q->sendEvent(oldActiveFocusItem, &event);
+            activeFocusItem = 0;
+            QFocusEvent event(QEvent::FocusOut, Qt::OtherFocusReason);
+            q->sendEvent(oldActiveFocusItem, &event);
 
-        QSGItem *afi = oldActiveFocusItem;
-        while (afi != scope) {
-            if (QSGItemPrivate::get(afi)->activeFocus) {
-                QSGItemPrivate::get(afi)->activeFocus = false;
-                changed << afi;
+            QSGItem *afi = oldActiveFocusItem;
+            while (afi != scope) {
+                if (QSGItemPrivate::get(afi)->activeFocus) {
+                    QSGItemPrivate::get(afi)->activeFocus = false;
+                    changed << afi;
+                }
+                afi = afi->parentItem();
             }
-            afi = afi->parentItem();
         }
     }
 
-    QSGItem *oldSubFocusItem = scopePrivate->subFocusItem;
-    // Correct focus chain in scope
-    if (oldSubFocusItem) {
-        QSGItem *sfi = scopePrivate->subFocusItem->parentItem();
-        while (sfi != scope) {
-            QSGItemPrivate::get(sfi)->subFocusItem = 0;
-            sfi = sfi->parentItem();
+    if (item != rootItem) {
+        QSGItem *oldSubFocusItem = scopePrivate->subFocusItem;
+        // Correct focus chain in scope
+        if (oldSubFocusItem) {
+            QSGItem *sfi = scopePrivate->subFocusItem->parentItem();
+            while (sfi != scope) {
+                QSGItemPrivate::get(sfi)->subFocusItem = 0;
+                sfi = sfi->parentItem();
+            }
         }
-    }
-    {
-        scopePrivate->subFocusItem = item;
-        QSGItem *sfi = scopePrivate->subFocusItem->parentItem();
-        while (sfi != scope) {
-            QSGItemPrivate::get(sfi)->subFocusItem = item;
-            sfi = sfi->parentItem();
+        {
+            scopePrivate->subFocusItem = item;
+            QSGItem *sfi = scopePrivate->subFocusItem->parentItem();
+            while (sfi != scope) {
+                QSGItemPrivate::get(sfi)->subFocusItem = item;
+                sfi = sfi->parentItem();
+            }
         }
-    }
 
-    if (oldSubFocusItem) {
-        QSGItemPrivate::get(oldSubFocusItem)->focus = false;
-        changed << oldSubFocusItem;
+        if (oldSubFocusItem) {
+            QSGItemPrivate::get(oldSubFocusItem)->focus = false;
+            changed << oldSubFocusItem;
+        }
     }
 
     if (!(options & DontChangeFocusProperty)) {
-        itemPrivate->focus = true;
-        changed << item;
+        if (item != rootItem || q->hasFocus()) {
+            itemPrivate->focus = true;
+            changed << item;
+        }
     }
 
-    if (newActiveFocusItem) {
+    if (newActiveFocusItem && q->hasFocus()) {
         activeFocusItem = newActiveFocusItem;
 
         QSGItemPrivate::get(newActiveFocusItem)->activeFocus = true;
         changed << newActiveFocusItem;
 
         QSGItem *afi = newActiveFocusItem->parentItem();
-        while (afi != scope) {
+        while (afi && afi != scope) {
             if (afi->isFocusScope()) {
                 QSGItemPrivate::get(afi)->activeFocus = true;
                 changed << afi;
@@ -730,7 +745,7 @@ void QSGCanvasPrivate::setFocusInScope(QSGItem *scope, QSGItem *item, FocusOptio
         updateInputMethodData();
     }
 
-    if (!changed.isEmpty()) 
+    if (!changed.isEmpty())
         notifyFocusChangesRecur(changed.data(), changed.count() - 1);
 }
 
@@ -740,7 +755,7 @@ void QSGCanvasPrivate::clearFocusInScope(QSGItem *scope, QSGItem *item, FocusOpt
 
     Q_UNUSED(item);
     Q_ASSERT(item);
-    Q_ASSERT(scope);
+    Q_ASSERT(scope || item == rootItem);
 
 #ifdef FOCUS_DEBUG
     qWarning() << "QSGCanvasPrivate::clearFocusInScope():";
@@ -749,20 +764,20 @@ void QSGCanvasPrivate::clearFocusInScope(QSGItem *scope, QSGItem *item, FocusOpt
     qWarning() << "    activeFocusItem:" << (QObject *)activeFocusItem;
 #endif
 
-    QSGItemPrivate *scopePrivate = QSGItemPrivate::get(scope);
+    QSGItemPrivate *scopePrivate = scope ? QSGItemPrivate::get(scope) : 0;
 
     QSGItem *oldActiveFocusItem = 0;
     QSGItem *newActiveFocusItem = 0;
 
     QVarLengthArray<QSGItem *, 20> changed;
 
-    Q_ASSERT(item == scopePrivate->subFocusItem);
+    Q_ASSERT(item == rootItem || item == scopePrivate->subFocusItem);
 
     // Does this change the active focus?
-    if (scopePrivate->activeFocus) {
+    if (item == rootItem || scopePrivate->activeFocus) {
         oldActiveFocusItem = activeFocusItem;
         newActiveFocusItem = scope;
-        
+
         Q_ASSERT(oldActiveFocusItem);
 
 #ifndef QT_NO_IM
@@ -784,20 +799,25 @@ void QSGCanvasPrivate::clearFocusInScope(QSGItem *scope, QSGItem *item, FocusOpt
         }
     }
 
-    QSGItem *oldSubFocusItem = scopePrivate->subFocusItem;
-    // Correct focus chain in scope
-    if (oldSubFocusItem) {
-        QSGItem *sfi = scopePrivate->subFocusItem->parentItem();
-        while (sfi != scope) {
-            QSGItemPrivate::get(sfi)->subFocusItem = 0;
-            sfi = sfi->parentItem();
+    if (item != rootItem) {
+        QSGItem *oldSubFocusItem = scopePrivate->subFocusItem;
+        // Correct focus chain in scope
+        if (oldSubFocusItem) {
+            QSGItem *sfi = scopePrivate->subFocusItem->parentItem();
+            while (sfi != scope) {
+                QSGItemPrivate::get(sfi)->subFocusItem = 0;
+                sfi = sfi->parentItem();
+            }
         }
-    }
-    scopePrivate->subFocusItem = 0;
+        scopePrivate->subFocusItem = 0;
 
-    if (oldSubFocusItem && !(options & DontChangeFocusProperty)) {
-        QSGItemPrivate::get(oldSubFocusItem)->focus = false;
-        changed << oldSubFocusItem;
+        if (oldSubFocusItem && !(options & DontChangeFocusProperty)) {
+            QSGItemPrivate::get(oldSubFocusItem)->focus = false;
+            changed << oldSubFocusItem;
+        }
+    } else if (!(options & DontChangeFocusProperty)) {
+        QSGItemPrivate::get(item)->focus = false;
+        changed << item;
     }
 
     if (newActiveFocusItem) {
@@ -1043,22 +1063,25 @@ bool QSGCanvas::event(QEvent *e)
 void QSGCanvas::keyPressEvent(QKeyEvent *e)
 {
     Q_D(QSGCanvas);
-    
-    sendEvent(d->activeFocusItem, e);
+
+    if (d->activeFocusItem)
+        sendEvent(d->activeFocusItem, e);
 }
 
 void QSGCanvas::keyReleaseEvent(QKeyEvent *e)
 {
     Q_D(QSGCanvas);
-    
-    sendEvent(d->activeFocusItem, e);
+
+    if (d->activeFocusItem)
+        sendEvent(d->activeFocusItem, e);
 }
 
 void QSGCanvas::inputMethodEvent(QInputMethodEvent *e)
 {
     Q_D(QSGCanvas);
 
-    sendEvent(d->activeFocusItem, e);
+    if (d->activeFocusItem)
+        sendEvent(d->activeFocusItem, e);
 }
 
 bool QSGCanvasPrivate::deliverInitialMousePressEvent(QSGItem *item, QGraphicsSceneMouseEvent *event)
diff --git a/src/declarative/items/qsgcanvas.h b/src/declarative/items/qsgcanvas.h
index 54701cae4f588e54a73c6f77e3e68a53daedc851..6707d24b3086c54e2edd60bfa17c91356a36e742 100644
--- a/src/declarative/items/qsgcanvas.h
+++ b/src/declarative/items/qsgcanvas.h
@@ -88,6 +88,9 @@ protected:
     virtual void showEvent(QShowEvent *);
     virtual void hideEvent(QHideEvent *);
 
+    virtual void focusOutEvent(QFocusEvent *);
+    virtual void focusInEvent(QFocusEvent *);
+
     virtual bool event(QEvent *);
     virtual void keyPressEvent(QKeyEvent *);
     virtual void keyReleaseEvent(QKeyEvent *);
diff --git a/tests/auto/declarative/qsgfocusscope/data/canvasFocus.qml b/tests/auto/declarative/qsgfocusscope/data/canvasFocus.qml
new file mode 100644
index 0000000000000000000000000000000000000000..7d8dac5a223f4de7008ce6bcce40ba8e237a08ee
--- /dev/null
+++ b/tests/auto/declarative/qsgfocusscope/data/canvasFocus.qml
@@ -0,0 +1,22 @@
+import QtQuick 2.0
+
+Column {
+    FocusScope {
+        objectName: "scope1"
+        width: 20 ;height: 20
+        focus: true
+        Rectangle {
+            objectName: "item1"
+            anchors.fill: parent
+            focus: true
+        }
+    }
+    FocusScope {
+        objectName: "scope2"
+        width: 20 ;height: 20
+        Rectangle {
+            objectName: "item2"
+            anchors.fill: parent
+        }
+    }
+}
diff --git a/tests/auto/declarative/qsgfocusscope/tst_qsgfocusscope.cpp b/tests/auto/declarative/qsgfocusscope/tst_qsgfocusscope.cpp
index 98ed425b1dcc1b14dc4226b8fe4fc22020fa9d9c..bd52052e21d2f0494f4cd0f55dc0c66a6a105787 100644
--- a/tests/auto/declarative/qsgfocusscope/tst_qsgfocusscope.cpp
+++ b/tests/auto/declarative/qsgfocusscope/tst_qsgfocusscope.cpp
@@ -47,6 +47,7 @@
 #include <private/qsgtextedit_p.h>
 #include <private/qsgtext_p.h>
 #include <QtDeclarative/private/qsgfocusscope_p.h>
+#include "../../../shared/util.h"
 #include <QtOpenGL/QGLShaderProgram>
 
 #ifdef Q_OS_SYMBIAN
@@ -75,6 +76,7 @@ private slots:
     void signalEmission();
     void qtBug13380();
     void forceActiveFocus();
+    void canvasFocus();
 };
 void tst_qsgfocusscope::initTestCase()
 {
@@ -347,6 +349,15 @@ void tst_qsgfocusscope::noParentFocus()
     view->setSource(QUrl::fromLocalFile(SRCDIR "/data/chain.qml"));
     QVERIFY(view->rootObject());
 
+    view->show();
+    qApp->setActiveWindow(view);
+    qApp->processEvents();
+
+#ifdef Q_WS_X11
+    // to be safe and avoid failing setFocus with window managers
+    qt_x11_wait_for_window_manager(view);
+#endif
+
     QVERIFY(view->rootObject()->property("focus1") == false);
     QVERIFY(view->rootObject()->property("focus2") == false);
     QVERIFY(view->rootObject()->property("focus3") == true);
@@ -445,6 +456,15 @@ void tst_qsgfocusscope::forceActiveFocus()
     QSGView *view = new QSGView;
     view->setSource(QUrl::fromLocalFile(SRCDIR "/data/forceActiveFocus.qml"));
 
+    view->show();
+    qApp->setActiveWindow(view);
+    qApp->processEvents();
+
+#ifdef Q_WS_X11
+    // to be safe and avoid failing setFocus with window managers
+    qt_x11_wait_for_window_manager(view);
+#endif
+
     QSGItem *rootObject = view->rootObject();
     QVERIFY(rootObject);
 
@@ -549,6 +569,135 @@ void tst_qsgfocusscope::forceActiveFocus()
     delete view;
 }
 
+void tst_qsgfocusscope::canvasFocus()
+{
+    QSGView *view = new QSGView;
+    view->setSource(QUrl::fromLocalFile(SRCDIR "/data/canvasFocus.qml"));
+
+    QSGItem *rootObject = view->rootObject();
+    QVERIFY(rootObject);
+
+    QSGItem *rootItem = view->rootItem();
+    QSGItem *scope1 = findItem<QSGItem>(rootObject, QLatin1String("scope1"));
+    QSGItem *item1 = findItem<QSGItem>(rootObject, QLatin1String("item1"));
+    QSGItem *scope2 = findItem<QSGItem>(rootObject, QLatin1String("scope2"));
+    QSGItem *item2 = findItem<QSGItem>(rootObject, QLatin1String("item2"));
+
+    QVERIFY(scope1);
+    QVERIFY(item1);
+    QVERIFY(scope2);
+    QVERIFY(item2);
+
+    QSignalSpy rootFocusSpy(rootItem, SIGNAL(focusChanged(bool)));
+    QSignalSpy scope1FocusSpy(scope1, SIGNAL(focusChanged(bool)));
+    QSignalSpy item1FocusSpy(item1, SIGNAL(focusChanged(bool)));
+    QSignalSpy scope2FocusSpy(scope2, SIGNAL(focusChanged(bool)));
+    QSignalSpy item2FocusSpy(item2, SIGNAL(focusChanged(bool)));
+    QSignalSpy rootActiveFocusSpy(rootItem, SIGNAL(activeFocusChanged(bool)));
+    QSignalSpy scope1ActiveFocusSpy(scope1, SIGNAL(activeFocusChanged(bool)));
+    QSignalSpy item1ActiveFocusSpy(item1, SIGNAL(activeFocusChanged(bool)));
+    QSignalSpy scope2ActiveFocusSpy(scope2, SIGNAL(activeFocusChanged(bool)));
+    QSignalSpy item2ActiveFocusSpy(item2, SIGNAL(activeFocusChanged(bool)));
+
+    // until the canvas widget has gained focus, no one should have active focus
+    QCOMPARE(view->hasFocus(), false);
+    QCOMPARE(rootItem->hasFocus(), false);
+    QCOMPARE(rootItem->hasActiveFocus(), false);
+    QCOMPARE(scope1->hasFocus(), true);
+    QCOMPARE(scope1->hasActiveFocus(), false);
+    QCOMPARE(item1->hasFocus(), true);
+    QCOMPARE(item1->hasActiveFocus(), false);
+    QCOMPARE(scope2->hasFocus(), false);
+    QCOMPARE(scope2->hasActiveFocus(), false);
+    QCOMPARE(item2->hasFocus(), false);
+    QCOMPARE(item2->hasActiveFocus(), false);
+
+    view->show();
+    qApp->setActiveWindow(view);
+    qApp->processEvents();
+
+#ifdef Q_WS_X11
+    // to be safe and avoid failing setFocus with window managers
+    qt_x11_wait_for_window_manager(view);
+#endif
+
+    // Now the canvas has focus, active focus given to item1
+    QTRY_COMPARE(view->hasFocus(), true);
+    QCOMPARE(rootItem->hasFocus(), true);
+    QCOMPARE(rootItem->hasActiveFocus(), true);
+    QCOMPARE(scope1->hasFocus(), true);
+    QCOMPARE(scope1->hasActiveFocus(), true);
+    QCOMPARE(item1->hasFocus(), true);
+    QCOMPARE(item1->hasActiveFocus(), true);
+    QCOMPARE(scope2->hasFocus(), false);
+    QCOMPARE(scope2->hasActiveFocus(), false);
+    QCOMPARE(item2->hasFocus(), false);
+    QCOMPARE(item2->hasActiveFocus(), false);
+    QCOMPARE(rootFocusSpy.count(), 1);
+    QCOMPARE(rootActiveFocusSpy.count(), 1);
+    QCOMPARE(scope1FocusSpy.count(), 0);
+    QCOMPARE(scope1ActiveFocusSpy.count(), 1);
+    QCOMPARE(item1FocusSpy.count(), 0);
+    QCOMPARE(item1ActiveFocusSpy.count(), 1);
+
+    view->clearFocus();
+    QCOMPARE(rootItem->hasFocus(), false);
+    QCOMPARE(rootItem->hasActiveFocus(), false);
+    QCOMPARE(scope1->hasFocus(), true);
+    QCOMPARE(scope1->hasActiveFocus(), false);
+    QCOMPARE(item1->hasFocus(), true);
+    QCOMPARE(item1->hasActiveFocus(), false);
+    QCOMPARE(rootFocusSpy.count(), 2);
+    QCOMPARE(rootActiveFocusSpy.count(), 2);
+    QCOMPARE(scope1FocusSpy.count(), 0);
+    QCOMPARE(scope1ActiveFocusSpy.count(), 2);
+    QCOMPARE(item1FocusSpy.count(), 0);
+    QCOMPARE(item1ActiveFocusSpy.count(), 2);
+
+    // canvas does not have focus, so item2 will not get active focus
+    item2->forceActiveFocus();
+
+    QCOMPARE(rootItem->hasFocus(), false);
+    QCOMPARE(rootItem->hasActiveFocus(), false);
+    QCOMPARE(scope1->hasFocus(), false);
+    QCOMPARE(scope1->hasActiveFocus(), false);
+    QCOMPARE(item1->hasFocus(), true);
+    QCOMPARE(item1->hasActiveFocus(), false);
+    QCOMPARE(scope2->hasFocus(), true);
+    QCOMPARE(scope2->hasActiveFocus(), false);
+    QCOMPARE(item2->hasFocus(), true);
+    QCOMPARE(item2->hasActiveFocus(), false);
+
+    QCOMPARE(rootFocusSpy.count(), 2);
+    QCOMPARE(rootActiveFocusSpy.count(), 2);
+    QCOMPARE(scope1FocusSpy.count(), 1);
+    QCOMPARE(scope1ActiveFocusSpy.count(), 2);
+    QCOMPARE(item1FocusSpy.count(), 0);
+    QCOMPARE(item1ActiveFocusSpy.count(), 2);
+    QCOMPARE(scope2FocusSpy.count(), 1);
+    QCOMPARE(scope2ActiveFocusSpy.count(), 0);
+    QCOMPARE(item2FocusSpy.count(), 1);
+    QCOMPARE(item2ActiveFocusSpy.count(), 0);
+
+    // give the canvas focus, and item2 will get active focus
+    view->setFocus();
+
+    QCOMPARE(rootItem->hasFocus(), true);
+    QCOMPARE(rootItem->hasActiveFocus(), true);
+    QCOMPARE(scope2->hasFocus(), true);
+    QCOMPARE(scope2->hasActiveFocus(), true);
+    QCOMPARE(item2->hasFocus(), true);
+    QCOMPARE(item2->hasActiveFocus(), true);
+    QCOMPARE(rootFocusSpy.count(), 3);
+    QCOMPARE(rootActiveFocusSpy.count(), 3);
+    QCOMPARE(scope2FocusSpy.count(), 1);
+    QCOMPARE(scope2ActiveFocusSpy.count(), 1);
+    QCOMPARE(item2FocusSpy.count(), 1);
+    QCOMPARE(item2ActiveFocusSpy.count(), 1);
+
+    delete view;
+}
+
 QTEST_MAIN(tst_qsgfocusscope)
 
 #include "tst_qsgfocusscope.moc"
diff --git a/tests/auto/declarative/qsgitem/tst_qsgitem.cpp b/tests/auto/declarative/qsgitem/tst_qsgitem.cpp
index 746b186c1e7caffbc9d01589d221c4e139bcd1f5..a59988ff1012cbe9f9cfc129ee6c2413f0442cc3 100644
--- a/tests/auto/declarative/qsgitem/tst_qsgitem.cpp
+++ b/tests/auto/declarative/qsgitem/tst_qsgitem.cpp
@@ -97,6 +97,18 @@ private slots:
     void enabled();
 
     void mouseGrab();
+
+private:
+    void ensureFocus(QWidget *w) {
+        w->show();
+        qApp->setActiveWindow(w);
+        qApp->processEvents();
+
+#ifdef Q_WS_X11
+        // to be safe and avoid failing setFocus with window managers
+        qt_x11_wait_for_window_manager(w);
+#endif
+    }
 };
 
 tst_qsgitem::tst_qsgitem()
@@ -192,6 +204,7 @@ struct FocusState : public QHash<QSGItem *, FocusData>
 void tst_qsgitem::simpleFocus()
 {
     QSGCanvas canvas;
+    ensureFocus(&canvas);
 
     QSGItem *l1c1 = new TestItem(canvas.rootItem());
     QSGItem *l1c2 = new TestItem(canvas.rootItem());
@@ -241,6 +254,7 @@ void tst_qsgitem::simpleFocus()
 void tst_qsgitem::scopedFocus()
 {
     QSGCanvas canvas;
+    ensureFocus(&canvas);
 
     QSGItem *l1c1 = new TestItem(canvas.rootItem());
     QSGItem *l1c2 = new TestItem(canvas.rootItem());
@@ -319,6 +333,7 @@ void tst_qsgitem::addedToCanvas()
 {
     {
     QSGCanvas canvas;
+    ensureFocus(&canvas);
 
     QSGItem *item = new TestItem;
 
@@ -337,6 +352,7 @@ void tst_qsgitem::addedToCanvas()
 
     {
     QSGCanvas canvas;
+    ensureFocus(&canvas);
 
     QSGItem *item = new TestItem(canvas.rootItem());
 
@@ -364,6 +380,7 @@ void tst_qsgitem::addedToCanvas()
 
     {
     QSGCanvas canvas;
+    ensureFocus(&canvas);
 
     QSGItem *tree = new TestItem;
     QSGItem *c1 = new TestItem(tree);
@@ -386,6 +403,7 @@ void tst_qsgitem::addedToCanvas()
 
     {
     QSGCanvas canvas;
+    ensureFocus(&canvas);
     QSGItem *tree = new TestFocusScope;
     QSGItem *c1 = new TestItem(tree);
     QSGItem *c2 = new TestItem(tree);
@@ -412,6 +430,7 @@ void tst_qsgitem::addedToCanvas()
 
     {
     QSGCanvas canvas;
+    ensureFocus(&canvas);
     QSGItem *tree = new TestFocusScope;
     QSGItem *c1 = new TestItem(tree);
     QSGItem *c2 = new TestItem(tree);
@@ -436,6 +455,7 @@ void tst_qsgitem::addedToCanvas()
 
     {
     QSGCanvas canvas;
+    ensureFocus(&canvas);
     QSGItem *child = new TestItem(canvas.rootItem());
     QSGItem *tree = new TestFocusScope;
     QSGItem *c1 = new TestItem(tree);
@@ -474,6 +494,7 @@ void tst_qsgitem::changeParent()
     // Parent to no parent
     {
     QSGCanvas canvas;
+    ensureFocus(&canvas);
     QSGItem *child = new TestItem(canvas.rootItem());
 
     FocusState focusState;
@@ -494,6 +515,7 @@ void tst_qsgitem::changeParent()
     // Different parent, same focus scope
     {
     QSGCanvas canvas;
+    ensureFocus(&canvas);
     QSGItem *child = new TestItem(canvas.rootItem());
     QSGItem *child2 = new TestItem(canvas.rootItem());
 
@@ -513,6 +535,7 @@ void tst_qsgitem::changeParent()
     // Different parent, different focus scope
     {
     QSGCanvas canvas;
+    ensureFocus(&canvas);
     QSGItem *child = new TestItem(canvas.rootItem());
     QSGItem *child2 = new TestFocusScope(canvas.rootItem());
     QSGItem *item = new TestItem(child);
@@ -533,6 +556,7 @@ void tst_qsgitem::changeParent()
     }
     {
     QSGCanvas canvas;
+    ensureFocus(&canvas);
     QSGItem *child = new TestItem(canvas.rootItem());
     QSGItem *child2 = new TestFocusScope(canvas.rootItem());
     QSGItem *item = new TestItem(child2);
@@ -553,6 +577,7 @@ void tst_qsgitem::changeParent()
     }
     {
     QSGCanvas canvas;
+    ensureFocus(&canvas);
     QSGItem *child = new TestItem(canvas.rootItem());
     QSGItem *child2 = new TestFocusScope(canvas.rootItem());
     QSGItem *item = new TestItem(child2);
diff --git a/tests/auto/declarative/qsgtextedit/tst_qsgtextedit.cpp b/tests/auto/declarative/qsgtextedit/tst_qsgtextedit.cpp
index 3e4803bc03e92d559142e02ad89473bd811fe7a1..7d74c0add8e5a5bafb76ef71c12fcf6efde03d50 100644
--- a/tests/auto/declarative/qsgtextedit/tst_qsgtextedit.cpp
+++ b/tests/auto/declarative/qsgtextedit/tst_qsgtextedit.cpp
@@ -2178,10 +2178,15 @@ void tst_qsgtextedit::preeditMicroFocus()
 
     QSGView view(QUrl::fromLocalFile(SRCDIR "/data/inputMethodEvent.qml"));
     MyInputContext ic;
+    // QSGCanvas won't set the Qt::WA_InputMethodEnabled flag unless a suitable item has focus
+    // and QWidget won't allow an input context to be set when the flag is not set.
+    view.setAttribute(Qt::WA_InputMethodEnabled, true);
     view.setInputContext(&ic);
+    view.setAttribute(Qt::WA_InputMethodEnabled, false);
     view.show();
     QApplication::setActiveWindow(&view);
     QTest::qWaitForWindowShown(&view);
+
     QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&view));
     QSGTextEdit *edit = qobject_cast<QSGTextEdit *>(view.rootObject());
     QVERIFY(edit);
@@ -2236,10 +2241,15 @@ void tst_qsgtextedit::inputContextMouseHandler()
 
     QSGView view(QUrl::fromLocalFile(SRCDIR "/data/inputContext.qml"));
     MyInputContext ic;
+    // QSGCanvas won't set the Qt::WA_InputMethodEnabled flag unless a suitable item has focus
+    // and QWidget won't allow an input context to be set when the flag is not set.
+    view.setAttribute(Qt::WA_InputMethodEnabled, true);
     view.setInputContext(&ic);
+    view.setAttribute(Qt::WA_InputMethodEnabled, false);
     view.show();
     QApplication::setActiveWindow(&view);
     QTest::qWaitForWindowShown(&view);
+
     QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&view));
     QSGTextEdit *edit = qobject_cast<QSGTextEdit *>(view.rootObject());
     QVERIFY(edit);
diff --git a/tests/auto/declarative/qsgtextinput/tst_qsgtextinput.cpp b/tests/auto/declarative/qsgtextinput/tst_qsgtextinput.cpp
index 015c47712af02bcbd65ae02f7d52bd6bd3ead7ae..1239abd0cf6ca646784384f7bee21219ee9d73e6 100644
--- a/tests/auto/declarative/qsgtextinput/tst_qsgtextinput.cpp
+++ b/tests/auto/declarative/qsgtextinput/tst_qsgtextinput.cpp
@@ -2197,7 +2197,11 @@ void tst_qsgtextinput::preeditAutoScroll()
 
     QSGView view(QUrl::fromLocalFile(SRCDIR "/data/preeditAutoScroll.qml"));
     MyInputContext ic;
+    // QSGCanvas won't set the Qt::WA_InputMethodEnabled flag unless a suitable item has active focus
+    // and QWidget won't allow an input context to be set when the flag is not set.
+    view.setAttribute(Qt::WA_InputMethodEnabled, true);
     view.setInputContext(&ic);
+    view.setAttribute(Qt::WA_InputMethodEnabled, false);
     view.show();
     QApplication::setActiveWindow(&view);
     QTest::qWaitForWindowShown(&view);
@@ -2262,7 +2266,11 @@ void tst_qsgtextinput::preeditMicroFocus()
 
     QSGView view(QUrl::fromLocalFile(SRCDIR "/data/inputMethodEvent.qml"));
     MyInputContext ic;
+    // QSGCanvas won't set the Qt::WA_InputMethodEnabled flag unless a suitable item has active focus
+    // and QWidget won't allow an input context to be set when the flag is not set.
+    view.setAttribute(Qt::WA_InputMethodEnabled, true);
     view.setInputContext(&ic);
+    view.setAttribute(Qt::WA_InputMethodEnabled, false);
     view.show();
     QApplication::setActiveWindow(&view);
     QTest::qWaitForWindowShown(&view);
@@ -2314,7 +2322,11 @@ void tst_qsgtextinput::inputContextMouseHandler()
 
     QSGView view(QUrl::fromLocalFile(SRCDIR "/data/inputContext.qml"));
     MyInputContext ic;
+    // QSGCanvas won't set the Qt::WA_InputMethodEnabled flag unless a suitable item has active focus
+    // and QWidget won't allow an input context to be set when the flag is not set.
+    view.setAttribute(Qt::WA_InputMethodEnabled, true);
     view.setInputContext(&ic);
+    view.setAttribute(Qt::WA_InputMethodEnabled, false);
     view.show();
     QApplication::setActiveWindow(&view);
     QTest::qWaitForWindowShown(&view);