Disable window shortcuts if there is a window modal dialog

If a window is blocked by a WindowModal dialog, it should not be
possible to trigger window shortcuts on that window if it receives
a WindowActivate event.

This currently happens if the blocked window gets clicked, because the
window becomes the active_window and then QApplication sends it a
WindowActivate event (this doesn't happen with application modal dialogs).

The correctWidgetContext() function calls QApplicationPrivate::tryModalHelper()
only if the shortcut context is ApplicationShortcut. This patch makes it
call even if the shortcut context is WindowShortcut.

Change-Id: Iff87d85bcae603a6a24128e0cedfa9d33b6485fd
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
Elvis Angelaccio 2017-07-29 11:28:13 +02:00
parent 8f1277da8c
commit a4f9cf2344
2 changed files with 52 additions and 2 deletions

View File

@ -205,7 +205,7 @@ static bool correctWidgetContext(Qt::ShortcutContext context, QWidget *w, QWidge
#if defined(DEBUG_QSHORTCUTMAP)
qDebug().nospace() << "..true [Pass-through]";
#endif
return true;
return QApplicationPrivate::tryModalHelper(w, nullptr);
}
#if QT_CONFIG(graphicsview)

View File

@ -26,7 +26,8 @@
**
****************************************************************************/
#include <QDialog>
#include <QMainWindow>
#include <QtTest/QtTest>
#include <qapplication.h>
@ -63,6 +64,8 @@ private slots:
void task229128TriggeredSignalWhenInActiongroup();
void repeat();
void setData();
void disableShortcutsWithBlockedWidgets_data();
void disableShortcutsWithBlockedWidgets();
private:
int m_lastEventType;
@ -424,5 +427,52 @@ void tst_QAction::setData() // QTBUG-62006
QCOMPARE(spy.count(), 1);
}
void tst_QAction::disableShortcutsWithBlockedWidgets_data()
{
QTest::addColumn<Qt::ShortcutContext>("shortcutContext");
QTest::addColumn<Qt::WindowModality>("windowModality");
QTest::newRow("application modal dialog should block window shortcut.")
<< Qt::WindowShortcut << Qt::ApplicationModal;
QTest::newRow("application modal dialog should block application shortcut.")
<< Qt::ApplicationShortcut << Qt::ApplicationModal;
QTest::newRow("window modal dialog should block application shortcut.")
<< Qt::ApplicationShortcut << Qt::WindowModal;
QTest::newRow("window modal dialog should block window shortcut.")
<< Qt::WindowShortcut << Qt::WindowModal;
}
void tst_QAction::disableShortcutsWithBlockedWidgets()
{
QMainWindow window;
QFETCH(Qt::ShortcutContext, shortcutContext);
QAction action(&window);
window.addAction(&action);
action.setShortcut(QKeySequence(Qt::Key_1));
action.setShortcutContext(shortcutContext);
window.show();
QVERIFY(QTest::qWaitForWindowExposed(&window));
QDialog dialog(&window);
QFETCH(Qt::WindowModality, windowModality);
dialog.setWindowModality(windowModality);
dialog.show();
QVERIFY(QTest::qWaitForWindowExposed(&dialog));
QApplication::setActiveWindow(&window);
QVERIFY(QTest::qWaitForWindowActive(&window));
QSignalSpy spy(&action, &QAction::triggered);
QTest::keyPress(&window, Qt::Key_1);
QCOMPARE(spy.count(), 0);
}
QTEST_MAIN(tst_QAction)
#include "tst_qaction.moc"