记住 PyQt 中 QTreeWidget 的滚动值

发布于 2025-01-07 12:45:47 字数 226 浏览 0 评论 0原文

我有一个从 XML 文件读取数据的 QTreeWidget。如果 XML 文件在任何时候发生更改(认为许多用户同时访问该文件),QTreeWidget 就会重新填充并重新读取。如果用户位于 QTreeWidget 顶部以外的任何位置(滚动方向),并且在清除后重新填充,则默认情况下它们会返回到顶部。

问题是,是否可以获取 QTreeWidget 的“滚动值”,然后在重新填充后返回到相同的值?如果是这样,怎么办?提前致谢!

I have a QTreeWidget that reads data from an XML file. If at any point the XML file is changed (think many users accessing said file at one time), the QTreeWidget is repopulated with clear and a re-read. If a user is anywhere but the top of the QTreeWidget, scroll-wise, and it gets repopulated after a clear, they're taken back to the top by default.

Question is, can one get the 'scroll value' of a QTreeWidget and then go back to that same value after a repopulation? If so, how? Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚殇 2025-01-14 12:45:47

您可以滚动到实际的先前值,就像您所要求的那样,但是您确定您的结果将始终具有相同的大小吗?这些数字对于将您再次带到正确的位置而言可能毫无意义。但仅供参考,您必须访问滚动条,获取其值,然后执行重新填充,然后再次滚动该值:

bar = treeWidget.verticalScrollBar()
yScroll = bar.value()
# repopulate here ...
treeWidget.scrollContentsBy(0, yScroll)

但更有用的方法是查找当前视图或感兴趣的项目,然后重新填充树,然后告诉树滚动到该实际项目。那么该项目现在存在于树中的哪个位置就不再重要了(如果数据结构发生了显着变化)。

首先按某些条件保存当前项目:

item = treeWidget.currentItem() # one way
item = treeWidget.itemAt(centerOfTree) # another way

# either save the text value or whatever the custom 
# identifying value is of your item
text = item.text()

一旦获得该数据值(无论是文本值还是其他自定义数据值),您就可以重新填充树,然后再次查找该项目。

# this is assuming the item is both present, 
# and referencing it by its string value
newItem = treeWidget.findItems(text)[0]
treeWidget.scrollToItem(newItem)

您可以修改它以适合您的实际项目类型。您可能会在项目上存储一些其他自定义值,以便再次找到它们。

You could scroll to the actual previous values, like you are asking, but are you sure your results will always be the same size? Those numbers could be meaningless in terms of taking you to the right spot again. But just for reference, you would have to access the scroll bar, take its value, then perform your repopulation, and then scroll that value again:

bar = treeWidget.verticalScrollBar()
yScroll = bar.value()
# repopulate here ...
treeWidget.scrollContentsBy(0, yScroll)

But a more useful approach would be to find the item that is current in view or of interest, then repopulate your tree, and then tell the tree to scroll to that actual item. Then it won't matter where in the tree the item now exists (if the data structure has changed significantly).

First save the current item by some criteria:

item = treeWidget.currentItem() # one way
item = treeWidget.itemAt(centerOfTree) # another way

# either save the text value or whatever the custom 
# identifying value is of your item
text = item.text()

Once you have that data value, be it the text value or some other custom data value, you can repopulate your tree, then look up that item again.

# this is assuming the item is both present, 
# and referencing it by its string value
newItem = treeWidget.findItems(text)[0]
treeWidget.scrollToItem(newItem)

You can modify this to suit your actual type of items. You may be storing some other custom value on the items to find them again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文