我有很少的组合框,在~100K行以内有很多挖掘数据集。我用QStandardItemModel
试了一下 - 如果模型是预加载的,那么工作得足够快,如果在单独的线程中执行模型加载也需要几秒钟。尝试使用QSqlQueryModel
组合框没有线程来提高性能,但是经历它比QStandardItemModel
慢得多(在我们的项目中QSqlQueryModel
使用QTreeView
这样的数据量非常快)。这可能是什么问题?有没有办法加速组合框,一些参数?
P.S。由Qt doc QComboBox::AdjustToMinimumContentsLengthWithIcon
建议不会加快速度:使用此类组合的对话开始时间太长并且退出10-20秒。 AdjustToMinimumContentsLength
的工作速度要快一点,但无论如何延迟都太长了。
答案 0 :(得分:2)
找到解决方案。第一个想法是找到哪个模型可以更快地工作,例如QStringListModel
来替换QStandardItemModel
或QSqlQueryModel
。然而,似乎它们的工作速度几乎相同。我在Qt doc中发现,默认情况下,combobox使用QStandardItemModel
来存储项目,QListView
子类显示弹出列表。您可以直接访问模型和视图(使用model()
和view()
)。这对我来说很奇怪,因为我知道QTreeView
对于更大量的数据工作得很好,而且从QListView
继承的更简单QAbstractItemView
也应该这样做。我开始挖掘QListView
并在其中找到解决了问题的属性:现在,combobox会立即打开大量数据。编写静态函数是为了调整所有这些组合(解释的注释来自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.
}