【问题标题】:how to set QTreeView background image with QStyle::StandardPixmap in stylesheet method?如何在样式表方法中使用 QStyle::StandardPixmap 设置 QTreeView 背景图像?
【发布时间】:2020-02-14 00:36:38
【问题描述】:

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

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

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

【问题讨论】:

    标签: pyqt pyqt5 qtstylesheets


    【解决方案1】:

    我建议快速上传 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_()) 
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 2022-07-11
    • 1970-01-01
    相关资源
    最近更新 更多