简体   繁体   English

QTreeWidget中稳定排序QTreeWidgetItems?

[英]stable sorting QTreeWidgetItems in QTreeWidget?

I have a list of QTreeWidgetItems (with children) in a QTreeWidget. 我在QTreeWidget中有一个QTreeWidgetItems列表(带有子项)。 I do not use a model for my data. 我没有为数据使用模型。

From another window in my application the user can navigate thru the same set of data (viewed differently) and the QTreeWidget in the first window then highlights that specific row by setting the background colour. 从应用程序的另一个窗口中,用户可以浏览同一组数据(以不同的方式查看),第一个窗口中的QTreeWidget然后通过设置背景色突出显示该特定行。

However, when the QTreeWidget is sorted on a column where some of the items have the same value it is undefined which item is first. 但是,当QTreeWidget在某些项目具有相同值的列上排序时,则不确定哪个是第一个项目。 When I then navigate using the other window and the background colour of the item is set, the equal items swaps place in the view automatically. 然后,当我使用其他窗口导航并设置了项目的背景色时,相等的项目会自动交换到视图中。 This looks very strange. 这看起来很奇怪。

I suspect this is due to the sorting algorithm of QTreeWidget, but does anyone know a possible workaround to this? 我怀疑这是由于QTreeWidget的排序算法引起的,但是没有人知道这可能的解决方法吗?

Are you using QItemSelectionModel to do this, or did you write it yourself? 您是使用QItemSelectionModel还是自己编写?

  • If you wrote it yourself I would suggest using QItemSelectionModel . 如果您自己编写,则建议使用QItemSelectionModel
  • If you didn't, it sounds like you want a custom sorting algorithm which would require creating a derived QTreeWidget , if you are doing that, you might as well just use QTreeView and a custom QAbstractItemModel . 如果不是这样,这听起来像您想要一个自定义排序算法,该算法需要创建一个派生的QTreeWidget ,如果这样做,则最好只使用QTreeView和一个自定义QAbstractItemModel

Also, if you have two views of the same data, I would HIGHLY recommended using the Model/View framework and a QTreeView . 另外,如果您对同一数据有两个视图,则强烈建议使用Model / View框架和QTreeView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM