diff options
-rw-r--r-- | src/gui/graphicsview/qgraphicsscene.cpp | 4 | ||||
-rw-r--r-- | tests/auto/declarative/qdeclarativeitem/data/mouseFocus.qml | 20 | ||||
-rw-r--r-- | tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp | 37 |
3 files changed, 59 insertions, 2 deletions
diff --git a/src/gui/graphicsview/qgraphicsscene.cpp b/src/gui/graphicsview/qgraphicsscene.cpp index dacdbfe..7abd5f6 100644 --- a/src/gui/graphicsview/qgraphicsscene.cpp +++ b/src/gui/graphicsview/qgraphicsscene.cpp @@ -1320,10 +1320,10 @@ void QGraphicsScenePrivate::mousePressEventHandler(QGraphicsSceneMouseEvent *mou setFocus = true; break; } - if (item->isEnabled() && ((item->flags() & QGraphicsItem::ItemIsFocusable) && item->d_ptr->mouseSetsFocus)) { + if (item->isEnabled() && ((item->flags() & QGraphicsItem::ItemIsFocusable))) { if (!item->isWidget() || ((QGraphicsWidget *)item)->focusPolicy() & Qt::ClickFocus) { setFocus = true; - if (item != q->focusItem()) + if (item != q->focusItem() && item->d_ptr->mouseSetsFocus) q->setFocusItem(item, Qt::MouseFocusReason); break; } diff --git a/tests/auto/declarative/qdeclarativeitem/data/mouseFocus.qml b/tests/auto/declarative/qdeclarativeitem/data/mouseFocus.qml new file mode 100644 index 0000000..a562b8b --- /dev/null +++ b/tests/auto/declarative/qdeclarativeitem/data/mouseFocus.qml @@ -0,0 +1,20 @@ +import Qt 4.7 + +QGraphicsWidget { + size: "200x100" + focusPolicy: QGraphicsWidget.ClickFocus + Item { + objectName: "declarativeItem" + id: item + width: 200 + height: 100 + MouseArea { + anchors.fill: parent + onPressed: { + if (!item.focus) { + item.focus = true; + } + } + } + } +} diff --git a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp index e0ca746..f4edeb2 100644 --- a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp +++ b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp @@ -45,6 +45,7 @@ #include <QtDeclarative/qdeclarativecontext.h> #include <QtDeclarative/qdeclarativeview.h> #include <QtDeclarative/qdeclarativeitem.h> +#include "../../../shared/util.h" class tst_QDeclarativeItem : public QObject @@ -67,6 +68,7 @@ private slots: void childrenProperty(); void resourcesProperty(); + void mouseFocus(); private: template<typename T> @@ -466,6 +468,41 @@ void tst_QDeclarativeItem::resourcesProperty() delete o; } +void tst_QDeclarativeItem::mouseFocus() +{ + QDeclarativeView *canvas = new QDeclarativeView(0); + QVERIFY(canvas); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/mouseFocus.qml")); + canvas->show(); + QVERIFY(canvas->rootObject()); + QApplication::setActiveWindow(canvas); + QTest::qWaitForWindowShown(canvas); + QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(canvas)); + + QDeclarativeItem *item = findItem<QDeclarativeItem>(canvas->rootObject(), "declarativeItem"); + QVERIFY(item); + QSignalSpy focusSpy(item, SIGNAL(focusChanged(bool))); + + QTest::mouseClick(canvas->viewport(), Qt::LeftButton, 0, canvas->mapFromScene(item->scenePos())); + QApplication::processEvents(); + QCOMPARE(focusSpy.count(), 1); + QVERIFY(item->hasFocus()); + + // make sure focusable graphics widget underneath does not steal focus + QTest::mouseClick(canvas->viewport(), Qt::LeftButton, 0, canvas->mapFromScene(item->scenePos())); + QApplication::processEvents(); + QCOMPARE(focusSpy.count(), 1); + QVERIFY(item->hasFocus()); + + item->setFocus(false); + QVERIFY(!item->hasFocus()); + QCOMPARE(focusSpy.count(), 2); + item->setFocus(true); + QCOMPARE(focusSpy.count(), 3); + + delete canvas; +} + void tst_QDeclarativeItem::propertyChanges() { QDeclarativeView *canvas = new QDeclarativeView(0); |