summaryrefslogtreecommitdiffstats
path: root/tests/auto/qgraphicsproxywidget
diff options
context:
space:
mode:
authorAlexis Menard <alexis.menard@nokia.com>2009-10-08 16:06:12 +0200
committerAlexis Menard <alexis.menard@nokia.com>2009-10-08 16:06:12 +0200
commit0054b444a202d08167c49a1a94fd2d306d14f91f (patch)
treeb10f1377906dbff9f8b6d7383be88a176086698c /tests/auto/qgraphicsproxywidget
parent922cf20905d829d1a4ec9205beacbee27efe35f8 (diff)
Fix context menu and menu key in QGraphicsView.
This fixes a weird bug when pressing the menu key on a proxy that embed a lineedit (but the proxy don't have the focus). At each time the key was pressed a context menu was created. When we press the menu key, QWidgetMapper will create a context menu event that will be sent to the QGraphicsView and will end up in QGraphicsScene. QGraphicsScene will then try to find all items under the mouse and will try to find any item from this list that accept this contextMenuEvent. In our case the proxy will always accept it and therefor will always send it to the widget that it owns and that's why we had this infinite numbers of QMenu. In QWidget world the context menu event is always sent to the widget that has the focus. If you checked any QWidget subclasses you'll see that all of them assume that when they get a context menu event everything is fine focus wise. We could have changed QGraphicsScene::contextMenuEvent but this will breaks some existing code. Instead we just checked that QGraphicsProxyWidget has the focus before we actually send the contextMenuEvent to the widget it owns. I have modified an existing auto-test to cover that. Be careful widget->setContextMenuPolicy(Qt::ActionsContextMenu) means that you will get no contextMenuEvent. :D. In this test i cover the standard use case and the one with Qt::ActionsContextMenu with and without the focus on the proxy. Task-number:QTBUG-3787 Reviewed-by:jan-arve
Diffstat (limited to 'tests/auto/qgraphicsproxywidget')
-rw-r--r--tests/auto/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp57
1 files changed, 46 insertions, 11 deletions
diff --git a/tests/auto/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp b/tests/auto/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp
index 76e7202b87..d8d97e89a6 100644
--- a/tests/auto/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp
+++ b/tests/auto/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp
@@ -170,6 +170,7 @@ private slots:
void dontCrashWhenDie();
void createProxyForChildWidget();
void actionsContextMenu();
+ void actionsContextMenu_data();
void deleteProxyForChildWidget();
void bypassGraphicsProxyWidget_data();
void bypassGraphicsProxyWidget();
@@ -3014,30 +3015,64 @@ private slots:
}
};
+void tst_QGraphicsProxyWidget::actionsContextMenu_data()
+{
+ QTest::addColumn<bool>("actionsContextMenu");
+ QTest::addColumn<bool>("hasFocus");
+
+ QTest::newRow("without actionsContextMenu and with focus") << false << true;
+ QTest::newRow("without actionsContextMenu and without focus") << false << false;
+ QTest::newRow("with actionsContextMenu and focus") << true << true;
+ QTest::newRow("with actionsContextMenu without focus") << true << false;
+}
+
void tst_QGraphicsProxyWidget::actionsContextMenu()
{
- ContextMenuWidget *widget = new ContextMenuWidget;
- widget->addAction(new QAction("item 1", widget));
- widget->addAction(new QAction("item 2", widget));
- widget->addAction(new QAction("item 3", widget));
- widget->setContextMenuPolicy(Qt::ActionsContextMenu);
+ QFETCH(bool, hasFocus);
+ QFETCH(bool, actionsContextMenu);
+ ContextMenuWidget *widget = new ContextMenuWidget;
+ if (actionsContextMenu) {
+ widget->addAction(new QAction("item 1", widget));
+ widget->addAction(new QAction("item 2", widget));
+ widget->addAction(new QAction("item 3", widget));
+ widget->setContextMenuPolicy(Qt::ActionsContextMenu);
+ }
QGraphicsScene scene;
- scene.addWidget(widget);
+
+ if (hasFocus)
+ scene.addWidget(widget)->setFocus();
+ else
+ scene.addWidget(widget)->clearFocus();
QGraphicsView view(&scene);
view.show();
-#ifdef Q_WS_X11
- qt_x11_wait_for_window_manager(&view);
-#endif
+ QTest::qWaitForWindowShown(&view);
+ view.setFocus();
QContextMenuEvent contextMenuEvent(QContextMenuEvent::Mouse,
view.viewport()->rect().center(),
view.viewport()->mapToGlobal(view.viewport()->rect().center()));
contextMenuEvent.accept();
qApp->sendEvent(view.viewport(), &contextMenuEvent);
- QVERIFY(widget->embeddedPopup);
- QVERIFY(!widget->gotContextMenuEvent);
+ QApplication::processEvents();
+
+ if (hasFocus) {
+ if (actionsContextMenu) {
+ //actionsContextMenu embedded popup but no contextMenuEvent (widget has focus)
+ QVERIFY(widget->embeddedPopup);
+ QVERIFY(!widget->gotContextMenuEvent);
+ } else {
+ //no embedded popup but contextMenuEvent (widget has focus)
+ QVERIFY(!widget->embeddedPopup);
+ QVERIFY(widget->gotContextMenuEvent);
+ }
+ } else {
+ //qgraphicsproxywidget doesn't have the focus, the widget must not receive any contextMenuEvent and must not create any QMenu
+ QVERIFY(!widget->embeddedPopup);
+ QVERIFY(!widget->gotContextMenuEvent);
+ }
+
}