QTreeView可以在PySide中修改它的header属性吗?

问题描述 投票:0回答:0

Python/PySide 程序员 我们可以修改QTreeView 的header 部分吗?所附图片应以红色圆圈显示。

理想情况下我想做的事:

  • 变色
  • 删除/隐藏大小和类型标题
  • 添加新列,如切换或复选框
  • 调整每个列的长度。 (我可以更改整个 treeView 的长度)

这是我的基本代码。如果你能把它写成一个单独的类,而不是类 TreeView(QTreeView) 下的一个方法,那就太棒了。

作为子问题,如果文件夹包含序列文件,例如: 图片A_v001.1001.jpg 图片A_v001.1002.jpg 图片A_v001.1003.jpg ...

是否可以像图片A_v001.(1000-1003).jpg之类的那样作为一个项目显示?

谢谢!! enter image description here

from PySide2.QtCore import Qt, QModelIndex, QUrl
from PySide2.QtGui import QDesktopServices
from PySide2.QtWidgets import QApplication, QMainWindow, QTreeView, QFileSystemModel, QMenu, QAction


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setGeometry(100, 100, 800, 600)

        self.model = QFileSystemModel()
        self.model.setRootPath('')
        self.tree_view = TreeView(self)
        self.tree_view.setModel(self.model)

        self.setCentralWidget(self.tree_view)




class TreeView(QTreeView):
    def __init__(self, parent=None):
        super(TreeView, self).__init__(parent)
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested.connect(self.show_context_menu)
        self.hideColumn(1)
    def show_context_menu(self, point):
        index = self.indexAt(point)
        if not index.isValid():
            return




class ApplysStyleSheet():
    def __init__(self) -> None:
        import os,subprocess    
        dirPath = os.path.dirname(__file__).replace('/','\\')
        dirPath = f'{dirPath}\style01.css'
        #print(f'dirPath is : {dirPath}')
        with open(dirPath, 'r') as f:
            self.style = f.read()
        #print(self.style)

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    app.setStyleSheet(ApplysStyleSheet().style)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

我尝试了一些方法,例如 self.hideColumn(1) 或 headerData() 之类的东西,但没有更改该标头上的任何内容。甚至没有错误信息显示。

我猜测使用 QFileSystemModel 可能是不可编辑的,而 QStandardItemModel 可能只能编辑标题。然而,最终我想创建一种双重浏览器,所以我应该坚持使用 QFileSystemModel。 (也许我错了,但 QStandardItemModel 需要手动创建和输入文件夹级别?!)

python header pyside qtreeview qfilesystemmodel
© www.soinside.com 2019 - 2024. All rights reserved.