Skip to content

Commit 3aff8c9

Browse files
committed
Browse Data: Use Tab to close autocomplete popup and move to next item
Fix #3743
1 parent e0f6aea commit 3aff8c9

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/ExtendedTableWidget.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ QWidget* ExtendedTableWidgetEditorDelegate::createEditor(QWidget* parent, const
185185
completer->setCompletionMode(QCompleter::PopupCompletion);
186186
completer->setCaseSensitivity(Qt::CaseInsensitive);
187187
editor->setCompleter(completer);
188+
189+
CompleterTabKeyPressedEventFilter* completerTabHandleFilter = new CompleterTabKeyPressedEventFilter(completer);
190+
completer->popup()->installEventFilter(completerTabHandleFilter);
188191
}
189192
// Set the maximum length to the highest possible value instead of the default 32768.
190193
editor->setMaxLength(std::numeric_limits<int>::max());

src/ExtendedTableWidget.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#ifndef EXTENDEDTABLEWIDGET_H
22
#define EXTENDEDTABLEWIDGET_H
33

4+
#include <QApplication>
5+
#include <QCompleter>
6+
#include <QEvent>
7+
#include <QKeyEvent>
48
#include <QTableView>
59
#include <QStyledItemDelegate>
610
#include <QSortFilterProxyModel>
@@ -30,6 +34,35 @@ class UniqueFilterModel : public QSortFilterProxyModel
3034
std::unordered_set<std::string> m_uniqueValues;
3135
};
3236

37+
// Handler for pressing the tab key when the autocomplete popup is open, closing the popup and moving the cursor to the next item.
38+
class CompleterTabKeyPressedEventFilter : public QObject
39+
{
40+
Q_OBJECT
41+
42+
public:
43+
CompleterTabKeyPressedEventFilter(QObject* parent = nullptr) : QObject(parent) {}
44+
45+
protected:
46+
bool eventFilter(QObject* obj, QEvent* event) override {
47+
if (event->type() == QEvent::KeyPress) {
48+
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
49+
if (keyEvent->key() == Qt::Key_Tab) {
50+
QCompleter* completer = qobject_cast<QCompleter*>(this->parent());
51+
if (completer) {
52+
completer->popup()->hide();
53+
QWidget* editor = completer->widget();
54+
if (editor) {
55+
QKeyEvent tabEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier);
56+
QApplication::sendEvent(editor, &tabEvent);
57+
}
58+
}
59+
return true;
60+
}
61+
}
62+
return QObject::eventFilter(obj, event);
63+
}
64+
};
65+
3366
// We use this class to provide editor widgets for the ExtendedTableWidget. It's used for every cell in the table view.
3467
class ExtendedTableWidgetEditorDelegate : public QStyledItemDelegate
3568
{

0 commit comments

Comments
 (0)