QCompleter and QListWidget as custom popup issue

3k Views Asked by At

I have a QCompleter using a QStringListModel for my QPlainTextEdit (check this example):

  QStringListModel* model = new QStringListModel(names);
  QCompleter* completer = new QCompleter(model);
  completer->setCompletionMode(QCompleter::PopupCompletion);
  completer->setModelSorting(QCompleter::UnsortedModel);

It works fine. Now I need some Icon, Tooltips for each suggestion I'm trying to use a QListWidget as custom popup:

  QListWidget* w = new QListWidget();
  foreach(name, names) {
    QListWidgetItem* i = new QListWidgetItem(name);
    i->setIcon(/*my Icon*/);
    i->setToolTip("");
    w->addItem(i);
  }
  completer->setPopup(w);

The popup ok, just like I need, but the completion no more work. I cannot type the text to make it filter the suggestion, just Up/Down key.
I have try:

  completer->setModel(w->model());

but no help!
What is my misstake or just QStringListModel give me the ability to filter the suggestions? What do you suggest?
Thanks you!

1

There are 1 best solutions below

0
On BEST ANSWER

I mostly deal with PyQt, but same deal. My syntax may be off, but you should use a QStandardItemModel vs. a QStringListModel. From there, you can leave it as the standard popup (QListView)

Something like:

QStandardItemModel* model = new QStandardItemModel();

// initialize the model
int rows = names.count();  // assuming this is a QStringList
model->setRowCount(rows);
model->setColumnCount(1);

// load the items
int row = 0;
foreach(name, names) {
    QStandardItem* item = new QStandardItem(name);
    item->setIcon(QIcon(":some/icon.png");
    item->setToolTip("some tool tip");
    model->setItem(row, 0, item);
    row++;
}

completer->setModel(model);
completer->popup()->setModel(model); // may or may not be needed