Fix window shortcuts when a completer popup has focus
The completer popup has focus, making QShortcut direct to it's window rather than to the window the completer belongs to. As QShortcut handles the case for Tool windows that have a parent, but doens't do the same for popups. And they shouldn't be treated the same way, as a context menu popup for a e.g. text edit should in fact block the text edit's shortcuts while open. However, the completer popup is special, in that it explicitly makes the widget completes for its focusProxy, which is what we can use to fix this issue. Change-Id: Ie7177d39668b3af14a1d9e0ee5d93eca9c67c8af Fixes: QTBUG-4485 Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
parent
f6db25962e
commit
0f3f143f6d
@ -189,10 +189,15 @@ static bool correctWidgetContext(Qt::ShortcutContext context, QWidget *w, QWidge
|
||||
}
|
||||
#endif
|
||||
|
||||
/* if a floating tool window is active, keep shortcuts on the
|
||||
* parent working */
|
||||
if (active_window != tlw && active_window && active_window->windowType() == Qt::Tool && active_window->parentWidget()) {
|
||||
active_window = active_window->parentWidget()->window();
|
||||
if (active_window && active_window != tlw) {
|
||||
/* if a floating tool window is active, keep shortcuts on the parent working.
|
||||
* and if a popup window is active (f.ex a completer), keep shortcuts on the
|
||||
* focus proxy working */
|
||||
if (active_window->windowType() == Qt::Tool && active_window->parentWidget()) {
|
||||
active_window = active_window->parentWidget()->window();
|
||||
} else if (active_window->windowType() == Qt::Popup && active_window->focusProxy()) {
|
||||
active_window = active_window->focusProxy()->window();
|
||||
}
|
||||
}
|
||||
|
||||
if (active_window != tlw) {
|
||||
|
@ -30,6 +30,8 @@
|
||||
#include <QtTest/QtTest>
|
||||
#include <qapplication.h>
|
||||
#include <qtextedit.h>
|
||||
#include <qlineedit.h>
|
||||
#include <qcompleter.h>
|
||||
#include <qpushbutton.h>
|
||||
#include <qmainwindow.h>
|
||||
#include <qstatusbar.h>
|
||||
@ -120,6 +122,7 @@ private slots:
|
||||
void unicodeCompare();
|
||||
void context();
|
||||
void duplicatedShortcutOverride();
|
||||
void shortcutToFocusProxy();
|
||||
|
||||
protected:
|
||||
static Qt::KeyboardModifiers toButtons( int key );
|
||||
@ -1279,5 +1282,28 @@ void tst_QShortcut::testElement()
|
||||
}
|
||||
}
|
||||
|
||||
void tst_QShortcut::shortcutToFocusProxy()
|
||||
{
|
||||
QLineEdit le;
|
||||
QCompleter completer;
|
||||
QStringListModel *slm = new QStringListModel(QStringList() << "a0" << "a1" << "a2", &completer);
|
||||
completer.setModel(slm);
|
||||
completer.setCompletionMode(QCompleter::PopupCompletion);
|
||||
le.setCompleter(&completer);
|
||||
QShortcut *shortcut = new QShortcut(QKeySequence(Qt::ALT + Qt::Key_S), &le);
|
||||
QObject::connect(shortcut, &QShortcut::activated, &le, &QLineEdit::clear);
|
||||
le.setFocus();
|
||||
le.show();
|
||||
|
||||
QVERIFY(QTest::qWaitForWindowActive(&le));
|
||||
QCOMPARE(QApplication::focusWidget(), &le);
|
||||
QTest::keyEvent(QTest::Press, QApplication::focusWidget(), Qt::Key_A);
|
||||
|
||||
QCOMPARE(le.text(), QString::fromLocal8Bit("a"));
|
||||
QTest::keyEvent(QTest::Press, QApplication::focusWidget(), Qt::Key_Alt);
|
||||
QTest::keyEvent(QTest::Press, QApplication::focusWidget(), Qt::Key_S, Qt::AltModifier);
|
||||
QCOMPARE(le.text(), QString());
|
||||
}
|
||||
|
||||
QTEST_MAIN(tst_QShortcut)
|
||||
#include "tst_qshortcut.moc"
|
||||
|
Loading…
x
Reference in New Issue
Block a user