How to use multi column model in QCompleter?
-
I've used the stock code editor example of Qt. In the code editor I've added these:
completer = new QCompleter(this); completer->setWidget(this); completer->setCaseSensitivity(Qt::CaseInsensitive); completer->setModel(model("://Keyword.txt"));
and the
model
function returns this:QStandardItemModel *CodeEditor::model(const QString &fileName) { QFile file(fileName); file.open(QIODevice::ReadOnly); auto model = new QStandardItemModel(completer); model->setColumnCount(2); while (!file.atEnd()){ auto line = file.readLine(); if (!line.isEmpty()){ auto col1 = new QStandardItem(QIcon(":/key.svg"), line.trimmed()); auto col2 = new QStandardItem("Keyword"); model->appendRow(QList<QStandardItem *>() << col1 << col2); } } file.close(); .... return model; }
So I've two columns in the model. First with an Icon and the second with static text. When the completer is triggered, it shows only the first column like this:
I want to show the second column and also want that column to be right aligned, grey font color and italic. How do you do that?
-
-
Hi,
You can create a QStyledItemDelegate that you set on the view used by the completer.
-
Because the widget set as popup is likely a QListView.
-
-
@SGaist, very close to what I wanted with QTableView. There's a small gap between row cells and it's clearly visible when an item is selected:
still couldn't find a way to remove that gap.
One more issue is, it doesn't resize to content size automatically. I've set the width manually
table->setFixedWidth(totalColumnWidth + 20)
. The 20 is for vertical scrollbar and when that becomes invisible there's another big gap at the end.