summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas Aardal Hanssen <andreas@hanssen.name>2012-03-07 19:26:17 (GMT)
committerQt by Nokia <qt-info@nokia.com>2012-03-19 07:49:04 (GMT)
commit66d8e41cbf607ef3f43d525ed8ec4ed376a6fac2 (patch)
tree655215524b9f4005d04487cfee48c318f3ff18d6
parent4f6c3d3f7b277bd0b0db1e06e72599eb45349886 (diff)
downloadQt-66d8e41cbf607ef3f43d525ed8ec4ed376a6fac2.zip
Qt-66d8e41cbf607ef3f43d525ed8ec4ed376a6fac2.tar.gz
Qt-66d8e41cbf607ef3f43d525ed8ec4ed376a6fac2.tar.bz2
Fix QDeclarativeItem::hasActiveFocus().
This function returns true if the item (or, in case it's a focus scope, one of its children,) has focus (i.e., will receive key events now), or will receive focus once the scene is activated. It also returns true if the item has not yet been added to a scene, but has subFocus, implicating that someone has called setFocus() on it prior to it being added to the scene; the latter case seen most commonly in unit tests. Cherry-picked from qt/qtquick1: bb364c14157df635cf166b293f8cab6c22534aa1 Task number: QTBUG-24681 Change-Id: I2f2d9dd81820e94202a44393a38972c868a774b7 Reviewed-by: Girish Ramakrishnan <girish.1.ramakrishnan@nokia.com> Reviewed-by: Alan Alpert <alan.alpert@nokia.com>
-rw-r--r--src/declarative/graphicsitems/qdeclarativeitem.cpp7
-rw-r--r--tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp28
2 files changed, 33 insertions, 2 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativeitem.cpp b/src/declarative/graphicsitems/qdeclarativeitem.cpp
index fa6e8e1..46070ea 100644
--- a/src/declarative/graphicsitems/qdeclarativeitem.cpp
+++ b/src/declarative/graphicsitems/qdeclarativeitem.cpp
@@ -3600,8 +3600,11 @@ void QDeclarativeItem::setSize(const QSizeF &size)
bool QDeclarativeItem::hasActiveFocus() const
{
Q_D(const QDeclarativeItem);
- return (focusItem() && focusItem()->isVisible()) && (focusItem() == this ||
- (d->flags & QGraphicsItem::ItemIsFocusScope && focusItem() != 0));
+ QGraphicsItem *fi = focusItem();
+ QGraphicsScene *s = scene();
+ bool hasOrWillGainFocus = fi && fi->isVisible() && (!s || s->focusItem() == fi);
+ bool isOrIsScopeOfFocusItem = (fi == this || (d->flags & QGraphicsItem::ItemIsFocusScope));
+ return hasOrWillGainFocus && isOrIsScopeOfFocusItem;
}
/*!
diff --git a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp
index 80a2233..870f03d 100644
--- a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp
+++ b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp
@@ -91,6 +91,7 @@ private slots:
void testQtQuick11Attributes_data();
void qtbug_16871();
void qtbug_21045();
+ void hasActiveFocusAfterClear();
private:
QDeclarativeEngine engine;
};
@@ -1251,6 +1252,33 @@ void tst_QDeclarativeItem::qtbug_21045()
QVERIFY(!i->hasActiveFocus());
}
+void tst_QDeclarativeItem::hasActiveFocusAfterClear()
+{
+ QGraphicsScene scene;
+ QGraphicsView view(&scene);
+ view.show();
+
+ QDeclarativeEngine engine;
+ QDeclarativeComponent qmlComponent(&engine);
+ qmlComponent.setData(
+ "import QtQuick 1.1;"
+ "TextInput {"
+ "width: 100; height: 100;"
+ "Rectangle { anchors.fill: parent; color: \"yellow\"; z: parent.z - 1 }"
+ "}", QUrl());
+ QDeclarativeItem *createdItem = qobject_cast<QDeclarativeItem*>(qmlComponent.create(engine.rootContext()));
+ QVERIFY(createdItem != 0);
+
+ scene.addItem(createdItem);
+
+ createdItem->QGraphicsItem::setFocus();
+ QCoreApplication::processEvents();
+ scene.setFocusItem(0);
+ QCoreApplication::processEvents();
+
+ QVERIFY(!createdItem->hasActiveFocus());
+}
+
QTEST_MAIN(tst_QDeclarativeItem)
#include "tst_qdeclarativeitem.moc"