QCombobox在使用大型QSqlQueryModel模型时速度非常慢

3
我有几个下拉列表,其中包含非常多的数据集,大约有100K行以上。我尝试使用 QStandardItemModel - 如果预加载模型,则运行速度足够快,如果在单独的线程中执行,则模型加载需要几秒钟时间。尝试了 QSqlQueryModel 的组合框,以改善性能,但是发现它比 QStandardItemModel 慢得多(在我们的项目中,使用 QTreeView 处理此类数据时, QSqlQueryModel 的工作速度非常快)。出了什么问题?是否有一种方法可以加速组合框,一些参数吗?
P.S. Qt文档建议使用 QComboBox :: AdjustToMinimumContentsLengthWithIcon 没有什么用:带有这些组合框的对话框启动太长,并且需要10-20秒才能退出。 AdjustToMinimumContentsLength 的速度稍微快一点,但无论如何延迟时间都太长了。

你真的需要一个有这么多选项的组合框吗? - hank
是的,用户应该从预定义的值中选择一个项目:其中一些是EMR值集 - 医疗应用程序,具有约10万个问题列表。 - Aleksey Kontsevich
问题可能出在所有模型都使用单个QSqlDatabase对象上吗?我应该为每个QSqlQueryModel使用单独的QSqlDatabase实例吗? - Aleksey Kontsevich
我有同样的问题。我认为QComboBox即使不显示它们也会创建所有数据视图。 - Paul-Sebastian Manole
Paul-Sebastian,您可以在此处找到完整解决方案:http://stackoverflow.com/a/33454284/630169 - Aleksey Kontsevich
1个回答

4

找到了解决方案。首先想到的是找到哪个模型可以更快地工作,例如用QStringListModel替换QStandardItemModelQSqlQueryModel。但似乎它们的速度几乎相同。第二,在Qt文档中发现,组合框默认使用QStandardItemModel存储项目,并且QListView子类显示弹出列表。您可以直接访问模型和视图(使用model()view())。这对我来说很奇怪,因为我知道QTreeView即使处理更大量的数据也能很好地工作,而更简单的QListView也继承自QAbstractItemView应该也能做到这一点。我开始挖掘QListView,并发现其中的属性已经解决了问题:现在在大量数据的情况下,组合框会立即打开。编写了一个静态函数来调整所有这样的组合框(带有Qt文档中的说明注释):

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.
}

只有一个问题留下了:如何加速自动完成器或让它在第二或第三个输入字符后打开? - Aleksey Kontsevich
这种情况的完整解决方案可以在此处找到:http://stackoverflow.com/a/33454284/630169 - Aleksey Kontsevich

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接