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 <marc.mutz@kdab.com>
This commit is contained in:
Mitch Curtis 2013-09-11 10:50:39 +02:00 committed by The Qt Project
parent 5da094c1aa
commit 3e549f5daa
2 changed files with 30 additions and 3 deletions

View File

@ -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);

View File

@ -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"