ComboBox: don't open on key release unless press was handled

If a Keys handler eats the press of the activation key, then the release
event shouldn't show the popup. The default keyPressEvent handler sets
the pressed state to true, so we can test that state to see if we should
act on the corresponding release. The key release event should still be
accepted to stop propagation.

Add a test case.

Pick-to: 6.5 6.2
Fixes: QTBUG-109721
Change-Id: Icb76453733d05cba59fdfb365b0cebf710be5f01
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
This commit is contained in:
Volker Hilsheimer 2023-04-17 21:11:16 +02:00
parent 2f757945b3
commit 3cbf7213df
2 changed files with 30 additions and 1 deletions

View File

@ -2064,7 +2064,7 @@ void QQuickComboBox::keyReleaseEvent(QKeyEvent *event)
if (!isEditable()) {
const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::ButtonPressKeys).value<QList<Qt::Key>>();
if (buttonPressKeys.contains(key)) {
if (!isEditable())
if (!isEditable() && isPressed())
d->togglePopup(true);
setPressed(false);
event->accept();

View File

@ -2277,6 +2277,35 @@ TestCase {
compare(control.currentIndex, 1)
}
// QTBUG-109721 - verify that an eaten press event for the space key
// doesn't open the popup when the key is released.
Component {
id: comboboxEatsSpace
ComboBox {
id: nonEditableComboBox
editable: false
model: ["NonEditable", "Delta", "Echo", "Foxtrot"]
Keys.onSpacePressed: (event) => event.accept
}
}
function test_spacePressEaten() {
let control = createTemporaryObject(comboboxEatsSpace, testCase)
verify(control)
control.forceActiveFocus()
var visibleChangedSpy = signalSpy.createObject(control, {target: control.popup, signalName: "visibleChanged"})
verify(visibleChangedSpy.valid)
// press doesn't open
keyPress(Qt.Key_Space)
verify(!control.pressed)
compare(visibleChangedSpy.count, 0)
// neither does release
keyRelease(Qt.Key_Space)
compare(visibleChangedSpy.count, 0)
}
Component {
id: listOfGadgets
QtObject {