【发布时间】:2021-03-25 19:36:01
【问题描述】:
我在下面使用 PySide2 代码和外部样式表。我想对所有对象使用相同的字体大小。我尝试在外部样式表中使用 * 选择器,(选择所有元素)。
相同的字体大小不适用于所有对象。我不明白我犯的错误。
我按照以下链接使用 .setObjectName("okButton") 解决了 # 选择器的问题。
但是 * 选择器不起作用。
https://doc.qt.io/qt-5/stylesheet-reference.html
Qt Stylesheets : Unable to use ID Selector
我的 PySide2 代码如下
import sys
from PySide2.QtWidgets import QWidget,QApplication,QPushButton
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.okButton = QPushButton("Ok",self)
self.okButton.setGeometry(30,50,100,25)
self.cancelButton = QPushButton("Cancel",self)
self.cancelButton.setGeometry(30,100,100,25)
self.abortButton = QPushButton("Abort",self)
self.abortButton.setGeometry(30,150,100,25)
self.okButton.setObjectName("okButton")
self.abortButton.setObjectName("abortButton")
self.abortButton.setStyleSheet("font-size: 14px;")
app = QApplication(sys.argv)
mystyle="style.qss"
with open(mystyle,"r") as ss:
app.setStyleSheet(ss.read())
window = MainWindow()
window.show()
sys.exit(app.exec_())
我的 QSS 脚本
QPushButton
{
color:white;
background-color:blue;
}
QPushButton#okButton
{
color:white;
background-color:red;
}
QPushButton#abortButton
{
color:white;
background-color:orange;
}
QPushButton *
{
font-size: 30px;
}
【问题讨论】:
-
为什么不把font-size放在
QPushButton下面? -
我需要对所有对象应用相同的字体,而不仅仅是 QPushbutton。我编辑了样式表
标签: python pyqt pyqt5 pyside pyside2