QTreeView: sync keyboard search behavior with other itemviews

QTreeView has a special implementation of keyboardSearch() which is not
consistent to the base implementation regarding the selection behavior:
 - currentSelectionStartIndex is not set which results in a wrong
   mouse selection behavior afterwards
 - only the current index is set but not the current selection
Sync the behavior by calling setCurrentIndex() directly in QTreeView
too.
One problem remains with this patch - the key handling is still
different between QAbstractItemView and QTreeView for repeating key
presses.

Task-number: QTBUG-18862
Change-Id: Ife79d146cf16d6ecbf9f86540777dae15aa1ffb0
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
bb10
Christian Ehrlicher 2018-09-07 21:08:48 +02:00
parent d0c159c8e3
commit 7fafeb4a34
2 changed files with 11 additions and 9 deletions

View File

@ -1088,15 +1088,8 @@ void QTreeView::keyboardSearch(const QString &search)
if (start.column() > 0)
index = index.sibling(index.row(), start.column());
if (index.isValid()) {
QItemSelectionModel::SelectionFlags flags = (d->selectionMode == SingleSelection
? QItemSelectionModel::SelectionFlags(
QItemSelectionModel::ClearAndSelect
|d->selectionBehaviorFlags())
: QItemSelectionModel::SelectionFlags(
QItemSelectionModel::NoUpdate));
selectionModel()->setCurrentIndex(index, flags);
}
if (index.isValid())
setCurrentIndex(index);
}
/*!

View File

@ -2435,6 +2435,8 @@ void tst_QTreeView::selection()
for (int i = 0;i < 10; ++i)
m.setData(m.index(i, 0), i);
treeView.setModel(&m);
treeView.show();
QVERIFY(QTest::qWaitForWindowExposed(&treeView));
treeView.setSelectionBehavior(QAbstractItemView::SelectRows);
treeView.setSelectionMode(QAbstractItemView::ExtendedSelection);
@ -2446,6 +2448,13 @@ void tst_QTreeView::selection()
QTest::mousePress(treeView.viewport(), Qt::LeftButton, 0, treeView.visualRect(m.index(1, 0)).center());
QTest::keyPress(treeView.viewport(), Qt::Key_Down);
auto selectedRows = treeView.selectionModel()->selectedRows();
QCOMPARE(selectedRows.size(), 1);
QCOMPARE(selectedRows.first(), m.index(2, 0, QModelIndex()));
QTest::keyPress(treeView.viewport(), Qt::Key_5);
selectedRows = treeView.selectionModel()->selectedRows();
QCOMPARE(selectedRows.size(), 1);
QCOMPARE(selectedRows.first(), m.index(5, 0, QModelIndex()));
}
//From task 151686 QTreeView ExtendedSelection selects hidden rows