gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:42:36 28 4
gpt4 key购买 nike

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

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

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

enter image description here

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

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

enter image description here

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

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

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

问题是什么?

最佳答案

我强烈建议您更仔细地阅读 style sheet syntaxreference文档,因为一切都明确指定并在那里解释:

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.

根据定义,样式表是级联

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

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

border: 1px solid black;

结果是它的所有子项都将具有该边框:您只给出了声明但没有选择器,所以universal 选择器用作隐式选择器。

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

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

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

关于python - PyQt - 如何正确使用 "setStyleSheet"方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67212507/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com