【问题标题】:PySide2, * selector, Select all elements is not working with external stylesheetPySide2,* 选择器,选择所有元素不适用于外部样式表
【发布时间】: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


【解决方案1】:

我找到了解决方案。我需要将样式细节放在 Asterisk 中,而不是任何对象名称。我用下面的脚本更正了上面的代码,我删除了 QPushButton 以便对所有对象应用相同的字体。

*
{
   property : value
}

【讨论】:

  • 我从 QPushButton * { font-size: 30px; } to * { 字体大小:30px; } 将相同的字体应用于所有对象而不是仅 QPushButton
  • 你可以在QPushButton 后面加一个逗号,这样也能正常工作
猜你喜欢
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
  • 2019-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-09
  • 2011-01-04
相关资源
最近更新 更多