Completion of values based on previous entered values in the same column

Using the QCompleter class associated to the QLineEdit, an implementation
of a completion of values in the Extended Table Widget is straightforward.
This commit is contained in:
mgrojo
2018-08-24 21:50:02 +02:00
committed by Martin Kleusberg
parent 517743ff3f
commit e1101ae690

View File

@@ -19,6 +19,7 @@
#include <QPrinter>
#include <QPrintDialog>
#include <QTextDocument>
#include <QCompleter>
#include <limits>
@@ -105,10 +106,15 @@ ExtendedTableWidgetEditorDelegate::ExtendedTableWidgetEditorDelegate(QObject* pa
{
}
QWidget* ExtendedTableWidgetEditorDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/) const
QWidget* ExtendedTableWidgetEditorDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& /*option*/, const QModelIndex& index) const
{
// Just create a normal line editor but set the maximum length to the highest possible value instead of the default 32768.
QLineEdit* editor = new QLineEdit(parent);
QCompleter *completer = new QCompleter(editor);
completer->setModel(const_cast<QAbstractItemModel*>(index.model()));
completer->setCompletionColumn(index.column());
completer->setCompletionMode(QCompleter::InlineCompletion);
editor->setCompleter(completer);
editor->setMaxLength(std::numeric_limits<int>::max());
return editor;
}