PyQt - 如何使用“setStyleSheet";方法正确吗? [英] PyQt - How to use the "setStyleSheet" method properly?

查看:47
本文介绍了PyQt - 如何使用“setStyleSheet";方法正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用 setStyleSheet 方法来更改特定小部件的样式,则放置在其中的其他小部件会更改其样式,但我不想要它!我可以举两个例子:

if I use the setStyleSheet method in order to change the style for a specific widget, the other ones placed inside it, changes their style, but I don't want it! I can bring you two example:

当我更改框架的边框/背景颜色时(请参阅放置在其中的小部件):

when I change the border/background color for a frame (see the widgets placed inside it):

import PyQt5.QtGui as qtg
import PyQt5.QtCore as qtc
import PyQt5.QtWidgets as qtw
import sys

class MainWindow(qtw.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
 
        self.resize(520,300)
        self.setWindowTitle("Treeview Example")

        self.layout = qtw.QVBoxLayout()

        self.frame1=qtw.QFrame()
        self.frame1layout=qtw.QVBoxLayout()
        self.frame1layout.setContentsMargins(5, 5, 5, 5)
        self.frame1.setLayout(self.frame1layout)
        self.frame1.setStyleSheet("border: 1px solid; border-color:red; background-color:white") # I change the style for the main frame
        self.layout.addWidget(self.frame1)
        
        self.frame2=qtw.QFrame()
        self.frame2layout=qtw.QVBoxLayout()
        self.frame2layout.setContentsMargins(10, 10, 10, 10)
        self.frame2.setLayout(self.frame2layout)
        
        self.frame1layout.addWidget(self.frame2)
        self.ProgressBar=qtw.QProgressBar()
        self.frame2layout.addWidget(self.ProgressBar)

        self.setLayout(self.layout)
 
if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

或者当我更改树视图小部件的边框颜色时(滚动的树视图小部件变为白色):

or when I change the border color for a treeview widget (the scrolled treeview widget is became white):

import PyQt5.QtGui as qtg
import PyQt5.QtCore as qtc
import PyQt5.QtWidgets as qtw
import sys

class MainWindow(qtw.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
 
        self.resize(520,300)
        self.setWindowTitle("Treeview Example")

        self.layout = qtw.QVBoxLayout()

        self.treeview = qtw.QTreeView(self)
        self.treeview.setStyleSheet("border: 1px solid; border-color:red") # it destroy the style of the objects inside the treeview widget!
        model = qtg.QStandardItemModel()
        rootNode = model.invisibleRootItem()
        
        section1 = qtg.QStandardItem("A")
        section1.appendRow([qtg.QStandardItem("A1")])
        childnode = qtg.QStandardItem("A2")
        section1.appendRow([childnode])
         
        section2 = qtg.QStandardItem("B")
        section2.appendRow([qtg.QStandardItem("B1")])
        section2.appendRow([qtg.QStandardItem("B2")])
        
        rootNode.appendRow([section1])
        rootNode.appendRow([section2])
        
        self.treeview.setHeaderHidden(True)
        
        self.treeview.setModel(model)
        
        self.layout.addWidget(self.treeview)
        self.setLayout(self.layout)
 
if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

我的问题是,如何在不修改其他小部件的样式的情况下更改特定小部件的样式?

my question is, how can I change the style of a specific widget without modifying the style of the other ones?

###########更新:

########### UPDATE:

在我的第一个例子中,如果我不使用指令 self.treeview.setStyleSheet("border: 1px solid; border-color:red") 我意识到进度条小部件不会像以前那样扩展.看这个截图:

in my first example, if I don't use the instruction self.treeview.setStyleSheet("border: 1px solid; border-color:red") I realized that the progress bar widget doesn't expand as before. see this screenshot:

有什么问题?

推荐答案

强烈建议大家仔细阅读样式表语法参考 文档,因为一切都明确在那里指定和解释:

I strongly suggest you to more carefully read the style sheet syntax and reference documentation, as everything is clearly specified and explained there:

样式表由一系列样式规则组成.样式规则由选择器和声明组成.selector 指定哪些小部件受规则影响;声明指定应该在小部件上设置哪些属性.

Style sheets consist of a sequence of style rules. A style rule is made up of a selector and a declaration. The selector specifies which widgets are affected by the rule; the declaration specifies which properties should be set on the widget.

根据定义,样式表是级联.

小部件上设置的样式表在其子部件上传播,这些子部件继承父部件的样式.

The stylesheet set on a widget is propagated on its children, those children inherit the style of the parent.

如果您像这样设置通用属性:

If you set a generic property like this:

border: 1px solid black;

结果是所有它的子级都有那个边框:你只给了声明,但没有选择器,所以universal 选择器用作隐式选择器.

The result is that all its children will have that border: you only gave the declaration but without the selector, so a universal selector is used as implicit.

这不仅仅是 Qt,这是典型的 CSS(QSS 的主要概念来自于它),它与任何其他小部件的样式属性完全一样:在小部件,将它们传播给它的所有孩子.
不同之处在于,对于样式表(与标准 CSS 完全一样),您可以使用 选择器.

This is not just Qt, this is typical of CSS (from which QSS take their main concepts), and it works exactly as any other widget styling property: setting a font or a palette on a widget, propagates them to all its children.
The difference is that with stylesheets (exactly like standard CSS) you can use selectors.

如果您只想设置树小部件的样式,请使用该类选择器:

If you want to style the tree widget only, then use that class selector:

    self.treeview.setStyleSheet('''
            QTreeView {
                border: 1px solid; 
                border-color:red;
            }
    ''')

这篇关于PyQt - 如何使用“setStyleSheet";方法正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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