aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Jones <martin.jones@nokia.com>2011-05-19 16:19:44 +1000
committerMartin Jones <martin.jones@nokia.com>2011-05-19 17:22:19 +1000
commitd774fc5f255fdd2443bd82a5fd87f4e4756e8a1b (patch)
treeaaa7b0ff98850af7d00bea1ee7fca3c7de0b6c16
parentaf5facf92d521cbfca9ceed50cce4b39b8f3d304 (diff)
Link item focus to canvas widget focus.
-rw-r--r--src/declarative/items/qsgcanvas.cpp153
-rw-r--r--src/declarative/items/qsgcanvas.h3
-rw-r--r--tests/auto/declarative/qsgfocusscope/data/canvasFocus.qml22
-rw-r--r--tests/auto/declarative/qsgfocusscope/tst_qsgfocusscope.cpp149
-rw-r--r--tests/auto/declarative/qsgitem/tst_qsgitem.cpp25
-rw-r--r--tests/auto/declarative/qsgtextedit/tst_qsgtextedit.cpp10
-rw-r--r--tests/auto/declarative/qsgtextinput/tst_qsgtextinput.cpp12
7 files changed, 309 insertions, 65 deletions
diff --git a/src/declarative/items/qsgcanvas.cpp b/src/declarative/items/qsgcanvas.cpp
index 6214da65c7..d17df05ee3 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 54701cae4f..6707d24b30 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 0000000000..7d8dac5a22
--- /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 98ed425b1d..bd52052e21 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 746b186c1e..a59988ff10 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 3e4803bc03..7d74c0add8 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 015c47712a..1239abd0cf 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);