问题
I have few comboboxes with very dig data sets within ~ 100K rows and more. I tried it with QStandardItemModel
- works fast enough if model is preloaded, also model loading takes few seconds if performed in separate thread. Tried comboboxes with QSqlQueryModel
without threading to improve performance but experienced it works much slower than QStandardItemModel
(in our project QSqlQueryModel
works very fast with such amount of data with QTreeView
for example). What could be the problem here? Is there a way to speed-up combobox, some parameters?
P.S. Suggested by Qt doc QComboBox::AdjustToMinimumContentsLengthWithIcon
does not speed things much: dialog with such combos starts too long and exits 10-20 sec. AdjustToMinimumContentsLength
works a little bit faster but anyway delays are too long.
回答1:
Found the solution. 1st thought was to find what model will work faster, for example QStringListModel
to replace QStandardItemModel
or QSqlQueryModel
. However seems that they work almost same speed. 2nd I found in Qt doc that combobox by default uses QStandardItemModel
to store the items and a QListView
subclass displays the popuplist. You can access the model and view directly (with model()
and view()
). That was strange for me as I know QTreeView
works just fine with even bigger amount of data, and simpler QListView
also inherited from QAbstractItemView
should do this as well. I start to digg into QListView
and found properties in it which had solved the problem: now combobox opens immediately on large amount of data. Static function was written to tweak all of such combos (comments with explanation are from Qt doc):
void ComboboxTools::tweak(QComboBox *combo)
{
// For performance reasons use this policy on large models
// or AdjustToMinimumContentsLengthWithIcon
combo->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
QListView *view = (QListView *)combo->view();
// Improving Performance: It is possible to give the view hints
// about the data it is handling in order to improve its performance
// when displaying large numbers of items. One approach that can be taken
// for views that are intended to display items with equal sizes
// is to set the uniformItemSizes property to true.
view->setUniformItemSizes(true);
// This property holds the layout mode for the items. When the mode is Batched,
// the items are laid out in batches of batchSize items, while processing events.
// This makes it possible to instantly view and interact with the visible items
// while the rest are being laid out.
view->setLayoutMode(QListView::Batched);
// batchSize : int
// This property holds the number of items laid out in each batch
// if layoutMode is set to Batched. The default value is 100.
}
来源:https://stackoverflow.com/questions/32750885/qcombobox-works-very-slow-with-qsqlquerymodel-with-large-model