gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:02:38 25 4
gpt4 key购买 nike

我正在使用 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_serverQWidget 的实例?

样式表只定义了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;} 

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

关于python - 为什么继承另一个类的类不会产生与 'another class' 相同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55416536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com