python - 使用 Qt 样式表的类实例的样式

标签 python pyqt5 qtstylesheets

我正在尝试为玩具 PyQt5 应用程序编写 CSS 样式表。我已经成功定义了一个类 MainWindow(QMainWindow)DefaultWidget(QWidget),后者包含两个按钮,我正在尝试对其进行样式化。

使用 C++ 文档,我一直在尝试利用 QPushButton#evilButton 示例,该示例提供于: Link

但是,实例名称的 # 表示法似乎并未转换为我的 Python 应用程序中外部 CSS 文件的类似 self.setStyleSheet() 方法.

Python 文件(如果有拼写错误,敬请谅解 - 从另一台计算机转录):

import sys
from PyQt5.Widgets import QApplication, QMainWindow, QWidget, QPushButton, QHBoxLayout

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setStyleSheet(open('./style.css').read())
        try:
            self.setCentralWidget(DefaultWidget())
        else:
            some_error_stuff_for_troubleshooting
        self.show()

class DefaultWidget(QWidget):
    def __init__(self):
        super().__init__()
        okButton = QPushButton('OK')
        cancelButton = QPushButton('Cancel')
        hbox = QHBoxLayout()
        hbox.addWidget(okButton)
        hbox.addWidget(cancelButton)
        self.setLayout(hbox)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

在 CSS 文件中...

QWidget.MainWindow {
    background: blue
}
QPushButton {
    background-color: beige
}
QPushButton???okButton {
    background-color: green
}

我把???放在哪里我尝试了很多事情(尝试遍历类QPushButton.okButtonQPushButton.DefaultWidget.okButton DefaultWidget.okButton、以及 .#::: 符号等),但此时它比受过教育的猜测更加随机。

我希望 CSS 能够在类名之外处理一些命名约定样式,以帮助从 python 文件中提取尽可能多的样式。即使符号碰巧是正确的,我是否没有看到由于继承的样式而导致的预期颜色变化?

<小时/>

编辑:如果有人偶然发现这一点,请使用 .setObjectName() 方法或在实例化小部件时声明对象名称选项。另外,如果您感兴趣的话,这里还有一个类似的问题,关于为什么不使用某种默认名称。 PyQt: is there an better way to set objectName in code?

最佳答案

尝试一下:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QHBoxLayout

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        #                       open('./style.css').read())
        self.setStyleSheet("""
            QWidget {
                background: blue;
            }
            QPushButton {
                background-color: beige;
            }
            QPushButton#okButton {                             /*    <--- #okButton      */
                background-color: green;
            }
        """)

        defaultWidget = DefaultWidget()
        self.setCentralWidget(defaultWidget)

        self.show()

class DefaultWidget(QWidget):
    def __init__(self):
        super().__init__()

        okButton = QPushButton('OK', objectName="okButton")     # + objectName="okButton"

        cancelButton = QPushButton('Cancel')
        hbox = QHBoxLayout()
        hbox.addWidget(okButton)
        hbox.addWidget(cancelButton)
        self.setLayout(hbox)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

enter image description here

关于python - 使用 Qt 样式表的类实例的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57779395/

相关文章:

python - 检测小部件窗口大小调整信号中的大小调整

python - 如何在不影响滚动条颜色的情况下更改 QScrollArea 的背景颜色?

python - 如何将WMA文件合并到mp3(带有标题编辑)?

python - 如何让 Python shell 找到 matplotlib?

python - TAB 键在我的 PYQT5 和 Python 代码中不起作用

python - 如何从另一个已经打开的窗口打开?

来自脚本的 python Nose ,从文件中收集测试类,然后运行测试

python - 删除numpy数组(矩阵)中的行: Delete previous k rows if value in column j equals k

qt - 使用样式表设置QGroupBox标题字体大小

python - 如何更改 QPushButton 文本和背景颜色