QbstractItemView: Allow Key_Space for keyboardSearch()

The keyboard search in QAbstractItemView did not handled Key_Space
because this is also an edit trigger. It was also consumed if no edit
was started.
This patch changes this behavior and triggers the keybaord search when
the editing was not started.

Fixes: QTBUG-48505
Change-Id: I58e0d283f863c9b12ac5d2f6171f15522bd7c30a
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Christian Ehrlicher 2018-12-15 20:42:18 +01:00
parent f568bfce64
commit 4c1313197b
2 changed files with 50 additions and 2 deletions

View File

@ -2438,8 +2438,14 @@ void QAbstractItemView::keyPressEvent(QKeyEvent *event)
break;
case Qt::Key_Space:
case Qt::Key_Select:
if (!edit(currentIndex(), AnyKeyPressed, event) && d->selectionModel)
d->selectionModel->select(currentIndex(), selectionCommand(currentIndex(), event));
if (!edit(currentIndex(), AnyKeyPressed, event)) {
if (d->selectionModel)
d->selectionModel->select(currentIndex(), selectionCommand(currentIndex(), event));
if (event->key() == Qt::Key_Space) {
keyboardSearch(event->text());
event->accept();
}
}
#ifdef QT_KEYPAD_NAVIGATION
if ( event->key()==Qt::Key_Select ) {
// Also do Key_Enter action.

View File

@ -91,6 +91,7 @@ private slots:
void itemWithHeaderItems();
void mimeData();
void selectedRowAfterSorting();
void search();
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
void clearItemData();
#endif
@ -164,6 +165,7 @@ void tst_QTableWidget::initTestCase()
{
testWidget = new QTableWidget();
testWidget->show();
QApplication::setKeyboardInputInterval(100);
}
void tst_QTableWidget::cleanupTestCase()
@ -1598,6 +1600,7 @@ public:
using QTableWidget::mimeData;
using QTableWidget::indexFromItem;
using QTableWidget::keyPressEvent;
};
void tst_QTableWidget::mimeData()
@ -1672,6 +1675,45 @@ void tst_QTableWidget::selectedRowAfterSorting()
}
}
void tst_QTableWidget::search()
{
auto createItem = [](const QString &txt)
{
auto item = new QTableWidgetItem(txt);
item->setFlags(item->flags().setFlag(Qt::ItemIsEditable, false));
return item;
};
auto checkSeries = [](TestTableWidget &tw, const QVector<QPair<QKeyEvent, int>> &series)
{
for (const auto &p : series) {
QKeyEvent e = p.first;
tw.keyPressEvent(&e);
QVERIFY(tw.selectionModel()->isSelected(tw.model()->index(p.second, 0)));
}
};
TestTableWidget tw(5, 1);
tw.setItem(0, 0, createItem("12"));
tw.setItem(1, 0, createItem("123"));
tw.setItem(2, 0, createItem("123 4"));
tw.setItem(3, 0, createItem("123 5"));
tw.setItem(4, 0, createItem(" "));
tw.show();
QKeyEvent evSpace(QEvent::KeyPress, Qt::Key_Space, Qt::NoModifier, " ");
QKeyEvent ev1(QEvent::KeyPress, Qt::Key_1, Qt::NoModifier, "1");
QKeyEvent ev2(QEvent::KeyPress, Qt::Key_2, Qt::NoModifier, "2");
QKeyEvent ev3(QEvent::KeyPress, Qt::Key_3, Qt::NoModifier, "3");
QKeyEvent ev4(QEvent::KeyPress, Qt::Key_4, Qt::NoModifier, "4");
QKeyEvent ev5(QEvent::KeyPress, Qt::Key_5, Qt::NoModifier, "5");
checkSeries(tw, {{evSpace, 4}, {ev1, 4}});
QTest::qWait(QApplication::keyboardInputInterval() * 2);
checkSeries(tw, {{ev1, 0}, {ev2, 0}, {ev3, 1}, {evSpace, 2}, {ev5, 3}});
QTest::qWait(QApplication::keyboardInputInterval() * 2);
checkSeries(tw, {{ev1, 0}, {ev2, 0}, {ev3, 1}, {evSpace, 2}, {ev4, 2}});
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
void tst_QTableWidget::clearItemData()
{