如何在样式表方法中使用 QStyle::StandardPixmap 设置 QTreeView 背景图像? [英] how to set QTreeView background image with QStyle::StandardPixmap in stylesheet method?

查看:63
本文介绍了如何在样式表方法中使用 QStyle::StandardPixmap 设置 QTreeView 背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);
}

推荐答案

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

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

这篇关于如何在样式表方法中使用 QStyle::StandardPixmap 设置 QTreeView 背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆