在 QTreeWidget 中插入时自动排序
我有一个 QTreeWidget,可以在其中插入项目,用户可以选择一列对其进行排序。 当插入项目时,它们只是附加到末尾,而不是自动完成排序。 如果我单击标题在升序/降序之间切换,它将对当前项目进行排序。
我想我可以调用 sortItems() 并使用从 sortColumn() 返回的列,但我无法看到如何查看用户是否正在进行升序或降序排序。
我并不担心这样做的效率,所以我不想等到插入完成后再进行排序。 我想要一个实时排序的列表。
谢谢!
I have a QTreeWidget that I insert items in, and the user can select a column to sort it. As the items are being inserted, they just get appended to the end instead of having the sort being done automatically. If I click the header to switch between ascending/descending it will sort the current items.
I figured I could call sortItems() and use the column that is returned from sortColumn(), but I am unable to see how I can see if the user is doing an ascending or descending sort.
I'm not worried about the efficiency of this, so I don't want to wait until the insertions are done and then do the sort. I'd like a real-time sorted list.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你的树部件被称为treeWidget,你应该能够调用 header() 方法,该方法来自 QTreeWidget 的父 QTreeView,然后调用来自 QHeaderView 类的 sortIndicatorOrder() :
有了这个,你就知道用户当前的排序顺序,并且你可以根据此对插入应用排序。
If your tree widget is called treeWidget, you should be able to call the header() method, which is from QTreeWidget's parent QTreeView, then sortIndicatorOrder() from the QHeaderView class:
With this, you know the user's current sort order, and you can apply your sort on insert according to this.
我没有测试设置,但根据文档,这应该会导致在插入项目时进行排序。
Qt 建议不要这样做,因为这会产生性能成本,并表示您应该在所有添加完成后启用排序。 希望这可以帮助。
I don't have a setup for testing but according to the documentation, this should cause sorting to occur as items are inserted.
Qt recommends against doing this as there will be a performance cost and say that you should set sorting enabled after all the adds are completed. Hope this helps.