为什么 class 继承另一个 class 不会产生与 'another class' 相同的结果?

Why does a class inhering another class not produce the same results as 'another class'?

我正在 Python 使用 PyQt5 开发一个应用程序。这是有问题的代码:

class Dialog(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.layout = QtWidgets.QGridLayout()
        self.main = QtWidgets.QWidget()
        self.main.setLayout(self.layout)

        self.setStyleSheet(QMainWindowStyle)
        self.setCentralWidget(self.main)
        self.show()

class AppearanceTab(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

class SettingsDialog(Dialog):
    def __init__(self):
        super().__init__()
        self.tabs = QtWidgets.QTabWidget(self)
        self.tabs.setStyleSheet(QTabWidgetStyle)
        self.layout.addWidget(self.tabs)

        self.tab_appearance = AppearanceTab()
        self.tab_appearance.setStyleSheet(QWidgetStyle)
        self.tab_appearance_layout = QtWidgets.QGridLayout()
        self.tab_appearance.setLayout(self.tab_appearance_layout)
        self.tabs.addTab(self.tab_appearance, "Appearance")

        self.tab_server = QtWidgets.QWidget()
        self.tab_server.setStyleSheet(QWidgetStyle)
        self.tab_server_layout = QtWidgets.QGridLayout()
        self.tab_server.setLayout(self.tab_server_layout)
        self.tabs.addTab(self.tab_server, "Server")

为什么当 self.tab_appearance 是一个 AppearanceTab 实例时( 应该QWidget 的副本)它有一个不同的当 self.tab_serverQWidget?

的实例时,将样式更改为 self.tab_server(即背景颜色发生变化)

样式表只定义了 background-color: #333333color: #dddddd.

提前致谢。

编辑:

我认为样式表没有正确应用于 AppearanceTab,但是我不知道为什么会这样,因为它只是继承自 QWidget

编辑 2:

可以找到一个 MCVE(连同我项目的其余部分)on github

在文档中,有一段 paragraph about inheritance 和样式:

Inheritance

In classic CSS, when font and color of an item is not explicitly set, it gets automatically inherited from the parent. When using Qt Style Sheets, a widget does not automatically inherit its font and color setting from its parent widget.

If we want to set the color on a QGroupBox and its children, we can write:

qApp->setStyleSheet("QGroupBox, QGroupBox * { color: red; }");

所以你可能想改变

QMainWindowStyle = QMainWindow {color: #dddddd; background-color: #333333;}

QMainWindowStyle = QMainWindow, QMainWindow * {color: #dddddd; background-color: #333333;} 

这样主 window 的所有子部件都具有相同的样式。

试一试:

from PyQt5 import QtWidgets

class Dialog(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.layout = QtWidgets.QGridLayout()
        self.main = QtWidgets.QWidget()
        self.main.setLayout(self.layout)

#        self.setStyleSheet(QMainWindowStyle)
        self.setCentralWidget(self.main)
        self.show()

class AppearanceTab(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

class SettingsDialog(Dialog):
    def __init__(self):
        super().__init__()
        self.tabs = QtWidgets.QTabWidget(self)
#        self.tabs.setStyleSheet(QTabWidgetStyle)
        self.layout.addWidget(self.tabs)

        self.tab_appearance = AppearanceTab()
#        self.tab_appearance.setStyleSheet(QWidgetStyle)
##        self.tab_appearance.setStyleSheet("QWidget, QWidget * {color: #dddddd; background-color: #333333;}") #note: Tried this however it didn't work.
        self.tab_appearance_layout = QtWidgets.QGridLayout()
        self.tab_appearance.setLayout(self.tab_appearance_layout)
        self.tabs.addTab(self.tab_appearance, "Appearance")

        self.tab_server = QtWidgets.QWidget()
#        self.tab_server.setStyleSheet(QWidgetStyle)
        self.tab_server_layout = QtWidgets.QGridLayout()
        self.tab_server.setLayout(self.tab_server_layout)
        self.tabs.addTab(self.tab_server, "Server")



style = """
QWidget {
    color: #dddddd;
    background-color: #333333;
}

QMainWindow {
    color: #dddddd;
    background-color: #333333;
}

QTabWidget {
    background-color: #333333;
    color: #dddddd;
}
QTabBar {
    color: #dddddd;
    background-color: #333333;
}
"""

if __name__ == "__main__":
    QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create("Fusion"))
    app = QtWidgets.QApplication([])
    app.setStyleSheet(style)                       # < ---
    d = SettingsDialog()
    print(app.exec_())