为什么一个类继承另一个类不会产生与“另一个类”相同的结果?

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


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_server(即背景颜色变化),当self.tab_server是一个实例QWidget?


样式表只定义background-color: #333333和color: #dddddd。


提前致谢。


编辑:


我相信样式表没有正确应用于AppearanceTab,但是我不知道为什么会看到它只是从QWidget.


编辑2:


可以在 github上找到 MCVE(以及我的项目的其余部分)。


狐的传说
浏览 179回答 2
2回答

千万里不及你

在文档中,有一段关于继承和样式的段落:遗产在经典 CSS 中,当未明确设置项目的字体和颜色时,它会自动从父项继承。使用 Qt 样式表时,小部件不会自动从其父小部件继承其字体和颜色设置。如果我们想在 QGroupBox 及其子项上设置颜色,我们可以这样写:qApp->setStyleSheet("QGroupBox, QGroupBox * { color: red; }");所以你可能想要改变QMainWindowStyle = QMainWindow {color: #dddddd; background-color: #333333;}到QMainWindowStyle = QMainWindow, QMainWindow * {color: #dddddd; background-color: #333333;}这样主窗口的所有子部件都具有相同的样式。

德玛西亚99

试试看:from PyQt5 import QtWidgetsclass Dialog(QtWidgets.QMainWindow):&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__()&nbsp; &nbsp; &nbsp; &nbsp; self.layout = QtWidgets.QGridLayout()&nbsp; &nbsp; &nbsp; &nbsp; self.main = QtWidgets.QWidget()&nbsp; &nbsp; &nbsp; &nbsp; self.main.setLayout(self.layout)#&nbsp; &nbsp; &nbsp; &nbsp; self.setStyleSheet(QMainWindowStyle)&nbsp; &nbsp; &nbsp; &nbsp; self.setCentralWidget(self.main)&nbsp; &nbsp; &nbsp; &nbsp; self.show()class AppearanceTab(QtWidgets.QWidget):&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__()class SettingsDialog(Dialog):&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__()&nbsp; &nbsp; &nbsp; &nbsp; self.tabs = QtWidgets.QTabWidget(self)#&nbsp; &nbsp; &nbsp; &nbsp; self.tabs.setStyleSheet(QTabWidgetStyle)&nbsp; &nbsp; &nbsp; &nbsp; self.layout.addWidget(self.tabs)&nbsp; &nbsp; &nbsp; &nbsp; self.tab_appearance = AppearanceTab()#&nbsp; &nbsp; &nbsp; &nbsp; self.tab_appearance.setStyleSheet(QWidgetStyle)##&nbsp; &nbsp; &nbsp; &nbsp; self.tab_appearance.setStyleSheet("QWidget, QWidget * {color: #dddddd; background-color: #333333;}") #note: Tried this however it didn't work.&nbsp; &nbsp; &nbsp; &nbsp; self.tab_appearance_layout = QtWidgets.QGridLayout()&nbsp; &nbsp; &nbsp; &nbsp; self.tab_appearance.setLayout(self.tab_appearance_layout)&nbsp; &nbsp; &nbsp; &nbsp; self.tabs.addTab(self.tab_appearance, "Appearance")&nbsp; &nbsp; &nbsp; &nbsp; self.tab_server = QtWidgets.QWidget()#&nbsp; &nbsp; &nbsp; &nbsp; self.tab_server.setStyleSheet(QWidgetStyle)&nbsp; &nbsp; &nbsp; &nbsp; self.tab_server_layout = QtWidgets.QGridLayout()&nbsp; &nbsp; &nbsp; &nbsp; self.tab_server.setLayout(self.tab_server_layout)&nbsp; &nbsp; &nbsp; &nbsp; self.tabs.addTab(self.tab_server, "Server")style = """QWidget {&nbsp; &nbsp; color: #dddddd;&nbsp; &nbsp; background-color: #333333;}QMainWindow {&nbsp; &nbsp; color: #dddddd;&nbsp; &nbsp; background-color: #333333;}QTabWidget {&nbsp; &nbsp; background-color: #333333;&nbsp; &nbsp; color: #dddddd;}QTabBar {&nbsp; &nbsp; color: #dddddd;&nbsp; &nbsp; background-color: #333333;}"""if __name__ == "__main__":&nbsp; &nbsp; QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create("Fusion"))&nbsp; &nbsp; app = QtWidgets.QApplication([])&nbsp; &nbsp; app.setStyleSheet(style)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# < ---&nbsp; &nbsp; d = SettingsDialog()&nbsp; &nbsp; print(app.exec_())
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python