0

i need to change QTreeView background image use QStyle::StandardPixmap with QT stylesheet , i try some method, but it don't work?

The stylesheet look like this, but it not working

QTreeView::branch:closed:adjoins-item {
    border-image: url(QStyle::SP_ArrowBack);
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
jett chen
  • 1,067
  • 16
  • 33

1 Answers1

1

I suggest quickly uploading QStyle.SP_ArrowBack to the file SP_ArrowBack.png , and then using it in the stylesheet.

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_()) 

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33
  • hi,i see qss at this [](https://doc.qt.io/qt-5/stylesheet-reference.html#list-of-icons ) page, how can i use system default icon without self picture.the method you provider is good, but my PC any picture is encrypted. – jett chen Oct 18 '19 at 00:47