Give Dialogs and Menus focus by default

Dialogs and Menus should always have focus when they're opened,
especially now that the cancel key handling in
QQuickPopup/QQuickPopupItem is done via regular key events and not
shortcuts, as popups will now require focus in order to be cancelled
via keyboard.

[ChangeLog][Controls][Important Behavior Changes] Dialogs and Menus now
have their focus property set to true by default. Popups are unchanged,
meaning that popups that should be interacted with via keyboard
(including cancelling) should have focus set to true or
forceActiveFocus() called on them after opening.

Task-number: QTBUG-79280
Change-Id: Idd070f2b1e8180a34a0dadb6e11dc56e531df211
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
Mitch Curtis 2022-02-25 16:43:45 +08:00
parent 615e9b3348
commit 7b535d22d6
4 changed files with 11 additions and 2 deletions

View File

@ -61,6 +61,8 @@ QT_BEGIN_NAMESPACE
\image qtquickcontrols2-page-wireframe.png
By default, Dialogs have \l focus.
\section1 Dialog Title and Buttons
Dialog's \l title is displayed by a style-specific title bar that is assigned
@ -203,6 +205,10 @@ QQuickDialog::QQuickDialog(QQuickDialogPrivate &dd, QObject *parent)
: QQuickPopup(dd, parent)
{
Q_D(QQuickDialog);
// Dialogs should get active focus when opened so that e.g. Cancel closes them.
setFocus(true);
QObject::connect(d->popupItem, &QQuickPopupItem::titleChanged, this, &QQuickDialog::titleChanged);
QObject::connect(d->popupItem, &QQuickPopupItem::headerChanged, this, &QQuickDialog::headerChanged);
QObject::connect(d->popupItem, &QQuickPopupItem::footerChanged, this, &QQuickDialog::footerChanged);

View File

@ -196,7 +196,7 @@ static const int SUBMENU_DELAY = 225;
will be \c true. For more information, see
\l {Keyboard Focus in Qt Quick}.
The default value is \c false.
The default value is \c true.
\sa {Popup::}{activeFocus}
*/

View File

@ -95,6 +95,7 @@ TestCase {
verify(control.header)
verify(control.footer)
compare(control.standardButtons, 0)
verify(control.focus)
}
function test_accept() {

View File

@ -117,6 +117,7 @@ void tst_QQuickMenu::defaults()
QQuickMenu *emptyMenu = helper.appWindow->property("emptyMenu").value<QQuickMenu*>();
QCOMPARE(emptyMenu->isVisible(), false);
QVERIFY(emptyMenu->hasFocus());
QCOMPARE(emptyMenu->currentIndex(), -1);
QCOMPARE(emptyMenu->contentItem()->property("currentIndex"), QVariant(-1));
QCOMPARE(emptyMenu->count(), 0);
@ -304,10 +305,11 @@ void tst_QQuickMenu::contextMenuKeyboard()
QVERIFY(firstItem);
QSignalSpy visibleSpy(menu, SIGNAL(visibleChanged()));
menu->setFocus(true);
QVERIFY(menu->hasFocus());
menu->open();
QCOMPARE(visibleSpy.count(), 1);
QVERIFY(menu->isVisible());
QVERIFY(menu->hasActiveFocus());
QQuickOverlay *overlay = window->property("overlay").value<QQuickOverlay*>();
QVERIFY(overlay);
QVERIFY(overlay->childItems().contains(menu->contentItem()->parentItem()));