Qtreewidget Pyqt5如何恢复正常?

How to restore to normal to Qtreewidget Pyqt5?

我刚刚用QTreeWidget.But做了一个程序,我想在点击按钮时恢复正常,我的意思是它应该和启动时一样。我学会了如何删除显示为蓝色水平 line.by

的选定部分
mytreeview.clearSelection()

查看图片 但是,如果有人扩大列宽,请检查第二张图片 ,“类型”列宽已从第一张增加 picture.Then 问题是我如何将其设置为正常方式以显示在我的默认值中picture-1 并想象有人通过拖动(位置交换)将“修改日期”的位置更改为“大小”列的位置。那么如何通过按下按钮将其重置为正常状态?这是对Qtreewidget的一些基本疑惑,所以我这里没有代码可写。 任何帮助将不胜感激 谢谢你

您可以使用 sectionSize() and restore it with resizeSection() 收集和存储部分大小。

为了恢复原来的逻辑索引,只需检查visualIndex() and if it doesn't match the logical index then use moveSection()

        # ...
        header = self.table.horizontalHeader()
        self.sectionSizes = [header.sectionSize(s) for s in range(header.count())]

    def resetSections(self):
        header = self.table.horizontalHeader()
        for section, size in enumerate(self.sectionSizes):
            if header.visualIndex(section) != section:
                header.moveSection(header.visualIndex(section), section)
            header.resizeSection(section, size)

我强烈建议您仔细阅读和学习有关QTableWidget (and its inherited classes: QTableView and QAbstractItemView) along with QHeaderView的文档,因为您所问的一切都在这些页面中得到了清楚的解释。