Performance QCombobox在使用大模型的QSqlQueryModel时运行非常慢

Performance QCombobox在使用大模型的QSqlQueryModel时运行非常慢,performance,model-view-controller,qt4,qcombobox,Performance,Model View Controller,Qt4,Qcombobox,我有几个组合框,其中包含大约100K行或更多行内的非常深入的数据集。我用QStandardItemModel尝试了它——如果模型是预加载的,那么工作速度足够快,如果在单独的线程中执行,那么模型加载也需要几秒钟的时间。尝试使用带有QSqlQueryModel的组合框而不使用线程来提高性能,但体验到它的工作速度比QStandardItemModel慢得多(例如,在我们的项目中,QSqlQueryModel使用QTreeView可以非常快速地处理如此大量的数据)。这里有什么问题?有没有办法加速组合框,

我有几个组合框,其中包含大约100K行或更多行内的非常深入的数据集。我用
QStandardItemModel
尝试了它——如果模型是预加载的,那么工作速度足够快,如果在单独的线程中执行,那么模型加载也需要几秒钟的时间。尝试使用带有
QSqlQueryModel
的组合框而不使用线程来提高性能,但体验到它的工作速度比
QStandardItemModel
慢得多(例如,在我们的项目中,
QSqlQueryModel
使用
QTreeView
可以非常快速地处理如此大量的数据)。这里有什么问题?有没有办法加速组合框,一些参数


Qt doc
QComboBox::AdjustToMinimumContentsLength WithIcon
建议的注意事项并不能加快速度:带有此类组合的对话框启动时间过长,退出时间为10-20秒
AdjustToMinimumContentsLength(代码>调整最小内容长度)
工作得快一点,但无论如何延迟太长。

找到了解决方案。第一个想法是找到工作更快的模型,例如用
QStringListModel
替换
QStandardItemModel
QSqlQueryModel
。然而,它们的工作速度似乎几乎相同。第二,我在Qt-doc中发现,combobox默认使用
QStandardItemModel
来存储项目,并且
QListView
子类显示POupList。您可以直接访问模型和视图(使用
model()
view()
)。这对我来说很奇怪,因为我知道
QTreeView
可以处理更大的数据量,而同样从
qabstractemview
继承的更简单的
QListView
也可以做到这一点。我开始钻研
QListView
,发现其中的属性解决了这个问题:现在组合框会在大量数据中立即打开。编写静态函数是为了调整所有这些组合(注释和解释来自Qt文档):


你真的需要一个包含这么多项目的组合框吗?是的,用户应该从预定义的值中选择一个项目:一些EMR值集-医疗应用程序,有大约100k个问题列表。问题可能存在于所有模型的单个QSqlDatabase对象中吗?我应该为每个QSqlQueryModel使用单独的QSqlDatabase实例吗?我有同样的问题。我认为QComboBox创建了所有的数据视图,即使它们没有显示出来。Paul-Sebastian,你可以在这里找到完整的解决方案:只剩下一个问题:如何加快完成器的速度,或者让它在第二个或第三个输入字符后打开?这种情况的完整解决方案可以在这里找到:
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.
}