Make keyboard search work in the current column, not only the first.

Task-number: QTBUG-26540
Change-Id: I1c365aeb013f5ddedd0589aa4c4844be759a3882
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
This commit is contained in:
Stephen Kelly 2012-08-22 16:18:11 +02:00 committed by Qt by Nokia
parent 0c955b4976
commit 1839151992
2 changed files with 35 additions and 0 deletions

View File

@ -1003,6 +1003,8 @@ void QTreeView::keyboardSearch(const QString &search)
for (int i = 0; i < d->viewItems.count(); ++i) {
if ((int)d->viewItems.at(i).level > previousLevel) {
QModelIndex searchFrom = d->viewItems.at(i).index;
if (start.column() > 0)
searchFrom = searchFrom.sibling(searchFrom.row(), start.column());
if (searchFrom.parent() == start.parent())
searchFrom = start;
QModelIndexList match = d->model->match(searchFrom, Qt::DisplayRole, searchString);
@ -1023,6 +1025,9 @@ void QTreeView::keyboardSearch(const QString &search)
else if (bestAbove > -1)
index = d->viewItems.at(bestAbove).index;
if (start.column() > 0)
index = index.sibling(index.row(), start.column());
if (index.isValid()) {
QItemSelectionModel::SelectionFlags flags = (d->selectionMode == SingleSelection
? QItemSelectionModel::SelectionFlags(

View File

@ -152,6 +152,7 @@ private slots:
void itemDelegate();
void itemDelegateForColumnOrRow();
void keyboardSearch();
void keyboardSearchMultiColumn();
void setModel();
void openPersistentEditor();
void rootIndex();
@ -1138,6 +1139,35 @@ void tst_QTreeView::keyboardSearch()
QVERIFY(view.selectionModel()->isSelected(model.index(1, 0)));
}
void tst_QTreeView::keyboardSearchMultiColumn()
{
QTreeView view;
QStandardItemModel model(4, 2);
model.setItem(0, 0, new QStandardItem("1")); model.setItem(0, 1, new QStandardItem("green"));
model.setItem(1, 0, new QStandardItem("bad")); model.setItem(1, 1, new QStandardItem("eggs"));
model.setItem(2, 0, new QStandardItem("moof")); model.setItem(2, 1, new QStandardItem("and"));
model.setItem(3, 0, new QStandardItem("elf")); model.setItem(3, 1, new QStandardItem("ham"));
view.setModel(&model);
view.show();
qApp->setActiveWindow(&view);
QVERIFY(QTest::qWaitForWindowActive(&view));
view.setCurrentIndex(model.index(0, 1));
// First item is selected
view.keyboardSearch(QLatin1String("eggs"));
QVERIFY(view.selectionModel()->isSelected(model.index(1, 1)));
QTest::qWait(QApplication::keyboardInputInterval() * 2);
// 'ham' is selected
view.keyboardSearch(QLatin1String("h"));
QVERIFY(view.selectionModel()->isSelected(model.index(3, 1)));
}
void tst_QTreeView::setModel()
{
QTreeView view;