admkrk
10th March 2017, 06:37
Hi, is there a way to filter out repeated strings when using a QSqlTableModel, in a QTableView as the model for a completer?
As an example, if I have a column, FirstName, that the completer is getting the list from, and there are 4 instances of Mark, I am currently getting 4 options for Mark in the popup. I would only like it to show one. Using inline, somewhat solves that, as I have it implemented below, but the popup option works better for my purposes. I have not tried, but it seems like inline does not correct for using lowercase when using that option, which would just add even more instances, assuming the database does not care about case. The only other alternative I can think of is to create a list, for each column, and apply that individually, according to the column being edited, which pretty much defeats the advantages of using a model.
If it helps, my delegate is created like this
QWidget *Delegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QLineEdit *editor = new QLineEdit(parent);
QCompleter *completer = new QCompleter(editor);
completer->setModel(model);
completer->setCompletionMode(QCompleter::InlineCompletion);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setCompletionColumn(index.column());
editor->setCompleter(completer);
return editor;
}
I get the model this way
Delegate::Delegate(QSqlTableModel *model, QObject *parent) :
QItemDelegate(parent)
{
this->model = model;
}
and the model, in mainWindow
model = new QSqlTableModel(this);
model->setTable("Component");
model->select();
ui->dataView->setModel(model);
ui->dataView->hideColumn(0);
ui->dataView->setSortingEnabled(true);
ui->dataView->setAlternatingRowColors(true);
delegate = new Delegate(model, this);
ui->dataView->setItemDelegate(delegate);
It might just be something simple I am overlooking, but I would appreciate any suggestions.
As an example, if I have a column, FirstName, that the completer is getting the list from, and there are 4 instances of Mark, I am currently getting 4 options for Mark in the popup. I would only like it to show one. Using inline, somewhat solves that, as I have it implemented below, but the popup option works better for my purposes. I have not tried, but it seems like inline does not correct for using lowercase when using that option, which would just add even more instances, assuming the database does not care about case. The only other alternative I can think of is to create a list, for each column, and apply that individually, according to the column being edited, which pretty much defeats the advantages of using a model.
If it helps, my delegate is created like this
QWidget *Delegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QLineEdit *editor = new QLineEdit(parent);
QCompleter *completer = new QCompleter(editor);
completer->setModel(model);
completer->setCompletionMode(QCompleter::InlineCompletion);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setCompletionColumn(index.column());
editor->setCompleter(completer);
return editor;
}
I get the model this way
Delegate::Delegate(QSqlTableModel *model, QObject *parent) :
QItemDelegate(parent)
{
this->model = model;
}
and the model, in mainWindow
model = new QSqlTableModel(this);
model->setTable("Component");
model->select();
ui->dataView->setModel(model);
ui->dataView->hideColumn(0);
ui->dataView->setSortingEnabled(true);
ui->dataView->setAlternatingRowColors(true);
delegate = new Delegate(model, this);
ui->dataView->setItemDelegate(delegate);
It might just be something simple I am overlooking, but I would appreciate any suggestions.