Filtering a QComboBox with no completion
-
My objective is to filter a QComboBox while typing. I want the matching entries to display in the QCombobox drop down but I do not want the QComboBox lineedit part to complete what I am typing.
I think I've been led down the wrong path by the following post:
https://stackoverflow.com/questions/32142411/filter-with-qcombobox-c
I am using a QStringListModel, QComboBox, QCompleter, and QSortFilterProxyModel. Here's what I should expect:
For simplicity let's assume that my QStringList contains:
{ "aaa", "aab", "abb", "bba", "bbb" }In the QComboBox if I type "a" the drop down should show the following without completing:
"aaa"
"aab"
"abb"If I continue typing "aa" drop down shows:
"aaa"
"aab"What I'm seeing now is when I type "aa" it auto-completes to "aaa" which I do not want. I don't want completion what so ever.
Here's my code.
QStringList strList = { "aaa", "aab", "abb", "bba", "bbb" } m_pModel = new QStringListModel(strList, this); m_pSortFilterProxyModel = new QSortFilterProxyModel(this); m_pSortFilterProxyModel->setSourceModel(m_pModel); m_pSortFilterProxyModel->setFilterCaseSensitivity(Qt::CaseSensitive); m_pComboBox->setModel(m_pSortFilterProxyModel); m_pCompleter = new QCompleter(m_pModel, this); m_pCompleter->setModel(m_pSortFilterProxyModel); m_pCompleter->setCompletionMode(QCompleter::PopupCompletion); m_pCompleter->setModelSorting(QCompleter::CaseSensitivelySortedModel); m_pCompleter->setFilterMode(Qt::MatchStartsWith); m_pCompleter->setCaseSensitivity(Qt::CaseSensitive); m_pComboBox->setCompleter(m_pCompleter); m_pComboBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); m_pComboBox->setMaxVisibleItems(20); m_pComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents); m_pComboBox->setInsertPolicy(QComboBox::NoInsert);
Here's the QComboBox text change slot:
// Slot to handle QComboBox text change void onFilterCombo(const QString &text) { QStringList items = m_pModel->stringList(); QString item; foreach(item, items) { if (item.indexOf(text) > -1) { m_pSortFilterProxyModel->setFilterFixedString(text); m_pComboBox->showPopup(); return; } } }
What I'm seeing is annoying text completion when I type and occasionally I see a crash when I type.
-
Hi,
One thing you can try is to call disconnect on the completer after you set it on your QComboBox.
Note that the completer already has sorting support. It might be enough for your needs.
-
I tried disconnecting the QComboBox
disconnect(m_pComboBox, 0, this, 0);
I also tried setting the completer to zero:
m_pComboBox->setCompleter(0);
Neither of these seem to work.
When I type on the QComboBox line edit I don't want to see anything else other than what I type.
-
I didn't suggest to disconnect the combobox but the completer.
Another possibility is to create your own subclass of QLineEdit.
-
This is actually a more critical issue than the completion issue..I am seeing a crash when I do filtering while typing. After looking around this and other forums I see that other Qt users are having the same problem. I have a custom slot for the text change event in my calling class:
// Slot to handle QComboBox text change void MyClass::onFilterCombo(const QString &text) { QStringList items = m_pModel->stringList(); QString item; foreach(item, items) { if (item.indexOf(text) > -1) { m_pSortFilterProxyModel->setFilterFixedString(text); m_pComboBox->showPopup(); return; } } }
Do you suggest updating the onFilterCombo slot above or creating a custom QSortFilterProxyModel class and reimplementing the slot to handle edit text change to resolve the crash. I'm open to any suggestions as I'm at a bit of an impasse right now.
-
What stack trace do you get from the crash ?