From 3e549f5daa55464512ee2763558f3a7ffd45c545 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Wed, 11 Sep 2013 10:50:39 +0200 Subject: QComboBox: fix keyboard selection with multiple character strings. Take longer search strings into account instead of just using the first character of the search string. Touches up https://qt.gitorious.org/qt/qt/merge_requests/1418 for resubmission. Task-number: QTBUG-3032 [ChangeLog][QtWidgets][QSpinBox] Fixed keyboard selection with multiple-character strings. Change-Id: I2f68c8b97b1a1884659dcb19f52b1efeace9b88b Reviewed-by: Marc Mutz --- src/widgets/itemviews/qabstractitemview.cpp | 3 +-- .../widgets/widgets/qcombobox/tst_qcombobox.cpp | 30 +++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp index 7edad74f54..771753b7da 100644 --- a/src/widgets/itemviews/qabstractitemview.cpp +++ b/src/widgets/itemviews/qabstractitemview.cpp @@ -2939,14 +2939,13 @@ void QAbstractItemView::keyboardSearch(const QString &search) } // search from start with wraparound - const QString searchString = sameKey ? QString(d->keyboardInput.at(0)) : d->keyboardInput; QModelIndex current = start; QModelIndexList match; QModelIndex firstMatch; QModelIndex startMatch; QModelIndexList previous; do { - match = d->model->match(current, Qt::DisplayRole, searchString); + match = d->model->match(current, Qt::DisplayRole, d->keyboardInput); if (match == previous) break; firstMatch = match.value(0); diff --git a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp index eabb7aaa17..509ccc37b6 100644 --- a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp +++ b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp @@ -162,6 +162,7 @@ private slots: void highlightedSignal(); void itemData(); void task_QTBUG_31146_popupCompletion(); + void keyboardSelection(); }; class MyAbstractItemDelegate : public QAbstractItemDelegate @@ -1848,7 +1849,7 @@ void tst_QComboBox::flaggedItems_data() itemList << "nine" << "ten"; keyMovementList << Qt::Key_T; - QTest::newRow(testCase.toLatin1() + "search same start letter") << itemList << deselectFlagList << disableFlagList << keyMovementList << bool(editable) << 9; + QTest::newRow(testCase.toLatin1() + "search same start letter") << itemList << deselectFlagList << disableFlagList << keyMovementList << bool(editable) << 2; keyMovementList.clear(); keyMovementList << Qt::Key_T << Qt::Key_H; @@ -2946,5 +2947,32 @@ void tst_QComboBox::task_QTBUG_31146_popupCompletion() QCOMPARE(comboBox.currentIndex(), 0); } +void tst_QComboBox::keyboardSelection() +{ + QComboBox comboBox; + const int keyboardInterval = QApplication::keyboardInputInterval(); + QStringList list; + list << "OA" << "OB" << "OC" << "OO" << "OP" << "PP"; + comboBox.addItems(list); + + // Clear any remaining keyboard input from previous tests. + QTest::qWait(keyboardInterval); + QTest::keyClicks(&comboBox, "oo", Qt::NoModifier, 50); + QCOMPARE(comboBox.currentText(), list.at(3)); + + QTest::qWait(keyboardInterval); + QTest::keyClicks(&comboBox, "op", Qt::NoModifier, 50); + QCOMPARE(comboBox.currentText(), list.at(4)); + + QTest::keyClick(&comboBox, Qt::Key_P, Qt::NoModifier, keyboardInterval); + QCOMPARE(comboBox.currentText(), list.at(5)); + + QTest::keyClick(&comboBox, Qt::Key_O, Qt::NoModifier, keyboardInterval); + QCOMPARE(comboBox.currentText(), list.at(0)); + + QTest::keyClick(&comboBox, Qt::Key_O, Qt::NoModifier, keyboardInterval); + QCOMPARE(comboBox.currentText(), list.at(1)); +} + QTEST_MAIN(tst_QComboBox) #include "tst_qcombobox.moc" -- cgit v1.2.3