PDA

View Full Version : QComboBox extremely slow on adding a lot of items



Buldozer
15th March 2012, 07:15
Slowly started porting my App from MFC to QT (4.7.4) and came across a nasty problem ... I have a ComboBox with A LOT of items (6k approx), and while you can easily find the item by making the ComboBox editable and auto-select kicks in, the populating of it takes A LONG time (4 sec approx on my Quad-core Xeon), which is quite unacceptable (the same ComboBox in MFC fills up unnoticeable). So - a question is - what can I do to fill it up faster? I guess addItems with a QStringList might be faster, but I need the itemData with each item, as I use that as an index into other data associated with each entry.

On the side-note is it possible to display the popup with items while entering the text in the QLineEdit of the QComboBox and move the selection on the popup according to data entered (more or less like the standard Windows ComboBox)?

I know that selecting an item in a 6k list is not really friendly, but it is data, that is usually known by name, and can thus be found fast by starting to type it in. I could try to group the items by areas and add another combo to select the area first (and then populate the other ComboBox with a subset of items), but that would mean that the user has two steps to do, where one is preferred.

Buldozer
15th March 2012, 16:24
Time to answer my own question (in case someone else comes across the same problem) ... Actually came across a solution while trying to use QTableWidget (which performance is even WAY worse). Same story actually - DON'T work with addItem, as it's really slow. Creating a QStandardItemModel, populating it with data and then attaching the model to the QComboBox with setModel works much faster. Not perfect but at least 6 times faster in my case, which brings it down to much more tolerable (though not perfect) 1 second.

MarekR22
24th September 2013, 10:54
Question is a bit old but I've found good solution (this question is quite high in proper google search).
Problem is a view used for presenting items. For some reason QListView is buggy and create all items representations even if they are not visible.
QTableView doesn't have such problem and it is possible to use this view in QComboBox: QComboBox::setView.
This will work like a charm, only problem is that table must be tweaked to be visually acceptable:

QTableView *table = new QTableView;
table->horizontalHeader()->setVisible(false);
table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
table->verticalHeader()->setVisible(false);
this->ui->someComboBox->setView(table);

gumtown
16th May 2021, 12:46
Thought I would share my results, my project uses a lot of pages of comboBoxes (100's) with lists up to 2000 items long.
I have suffered from very long executable startup times (40+ seconds) and have narrowed the issues down to the comboBoxes (currently Qt5.12).
When building the combo list using comboBox.addItem() seems to slow things A LOT !!

My successful result is to build the list using QList<QString> and then use comboBox.addItems(list);
This has speed up start time and page load times by a factor of about 5x