summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas Aardal Hanssen <andreas@hanssen.name>2012-03-07 20:26:17 +0100
committerThiago Macieira <thiago.macieira@intel.com>2012-04-05 15:45:00 +0200
commit95b6785cfd8e89012db3602ae92bf278df2b9300 (patch)
tree446fc00fc9f0bac0513ccefec30638496f5bebbe
parentaf66796c0327b1f215a843cedf03918fc40c4077 (diff)
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 (5.0): bb364c14157df635cf166b293f8cab6c22534aa1 Task number: QTBUG-24681 Change-Id: Ia1265158108d5d856b57e34a8efee2545cd0deeb Reviewed-by: Alan Alpert <alan.alpert@nokia.com> Reviewed-by: Girish Ramakrishnan <girish.1.ramakrishnan@nokia.com>
-rw-r--r--src/declarative/graphicsitems/qdeclarativeitem.cpp7
-rw-r--r--tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp41
2 files changed, 46 insertions, 2 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativeitem.cpp b/src/declarative/graphicsitems/qdeclarativeitem.cpp
index ee2d19d5f7..638e85044d 100644
--- a/src/declarative/graphicsitems/qdeclarativeitem.cpp
+++ b/src/declarative/graphicsitems/qdeclarativeitem.cpp
@@ -3564,8 +3564,11 @@ void QDeclarativeItem::setSize(const QSizeF &size)
bool QDeclarativeItem::hasActiveFocus() const
{
Q_D(const QDeclarativeItem);
- return 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 362a8ff722..a69fae92b9 100644
--- a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp
+++ b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp
@@ -90,6 +90,8 @@ private slots:
void testQtQuick11Attributes();
void testQtQuick11Attributes_data();
void qtbug_16871();
+ void qtbug_21045();
+ void hasActiveFocusAfterClear();
private:
QDeclarativeEngine engine;
};
@@ -1238,6 +1240,45 @@ void tst_QDeclarativeItem::qtbug_16871()
delete o;
}
+void tst_QDeclarativeItem::qtbug_21045()
+{
+ QDeclarativeComponent component(&engine);
+ QGraphicsScene scene;
+ component.setData("import QtQuick 1.1\nItem{visible: false; focus: true}", QUrl::fromLocalFile("file:"));
+ QObject *o = component.create();
+ QDeclarativeItem* i = qobject_cast<QDeclarativeItem*>(o);
+ QVERIFY(i);
+ scene.addItem(i);
+ 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"