如何在样式表方法中使用 QStyle::StandardPixmap 设置 QTreeView 背景图像?

how to set QTreeView background image with QStyle::StandardPixmap in stylesheet method?

我需要更改 QTreeView 背景图像使用 QStyle::StandardPixmap 和 QT 样式表,我尝试了一些方法,但它不起作用?

样式表看起来像这样,但它不起作用

QTreeView::branch:closed:adjoins-item {
    border-image: url(QStyle::SP_ArrowBack);
}

我建议快速上传 QStyle.SP_ArrowBack 到文件 SP_ArrowBack.png ,然后在样式表中使用它。

import sys
from PyQt5.QtWidgets import QTreeView, QFileSystemModel, QApplication, QStyle 
from PyQt5.QtCore    import QDir
from PyQt5.QtGui    import QIcon


class Tree(QTreeView):
    def __init__(self):
        QTreeView.__init__(self)
        self.setWindowIcon(self.style().standardIcon(QStyle.SP_ArrowBack))
### vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv        
        icon = self.style().standardIcon(QStyle.SP_ArrowBack)
        pixmap = icon.pixmap(200, 200, QIcon.Normal, QIcon.On)
        _icon = "{}.png".format("SP_ArrowBack")
        pixmap.save(_icon, quality = -1) 
        print("Icon loaded ...")

        self.setStyleSheet(""" 
        QTreeWidget {border:None;}

        QTreeWidget::item { height: 80px;
                           color: rgba(255,255,255,255);       
        }
        QTreeView {
            alternate-background-color: rgba(135,135,135,255);
            background: rgba(145,145,145,255);
        }            

        QTreeView::branch:has-siblings:!adjoins-item {
            border-image: url(vline.png) 0;                           
        }
        QTreeView::branch:has-siblings:adjoins-item {
            border-image: url(branch-more.png) 0;                    
        }
        QTreeView::branch:!has-children:!has-siblings:adjoins-item {
            border-image: url(branch-end.png) 0;                      
        }
        QTreeView::branch:has-children:!has-siblings:closed,
        QTreeView::branch:closed:has-children:has-siblings {
            border-image: none;
            image: url(%s);                                     /* <--- _icon */
        }
        QTreeView::branch:open:has-children:!has-siblings,
        QTreeView::branch:open:has-children:has-siblings {
            border-image: none;
            image: url(branch-open.png);                               
        }
        QTreeWidget::item[text="Header1"] {
            height: 80px;
        }
        QTreeWidget::item:children {
            height: 40px;
        }
        """ % _icon)               
### ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        

        model = QFileSystemModel()
        model.setRootPath(QDir.currentPath())
        self.setModel(model)
        self.setRootIndex(model.index(QDir.currentPath()))
        model.setReadOnly(True)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Tree() 
    w.resize(500, 300)    
    w.show()
    sys.exit(app.exec_())