PyQt - 如何正确使用 "setStyleSheet" 方法?

PyQt - How to use the "setStyleSheet" method properly?

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

当我更改框架的 border/background 颜色时(查看放置在其中的小部件):

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

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

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

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

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

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

问题是什么?

我强烈建议您更仔细地阅读 style sheet syntax and reference 文档,因为那里清楚地 指定并解释了所有内容:

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.

根据定义,样式表是级联

小部件上设置的样式表在其 children 上传播,那些 children 继承 parent 的样式。

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

border: 1px solid black;

结果是 all 它的 children 将有那个边框:你只给出了 声明 但没有 选择器,因此通用选择器用作隐式选择器。

这不仅仅是 Qt,这是 CSS 的典型特征(QSS 从中汲取了他们的主要概念),并且它的工作方式与任何其他小部件完全一样 样式 属性:在小部件上设置字体或调色板,将它们传播到其所有 children.
不同之处在于,对于样式表(与标准 CSS 完全一样),您可以使用 selectors.

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

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