【发布时间】:2022-08-16 04:42:55
【问题描述】:
我有这个应用程序,当单击主窗口中的按钮时,我有几个设置窗口打开。窗口是应用程序模式,因此一次只能打开一个。关于如何管理它们,我有两个想法,但我不确定哪一个是正确的方法。我并不特别关心这些值是如何存储的,只要我可以将它们传递到应用程序中的其他窗口并使用它们进行处理即可。
MainWindow 类选项 1:
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
central = QWidget()
layout = QVBoxLayout()
button = QPushButton(\'Show window\')
layout.addWidget(button)
window = OtherWindow()
button.clicked.connect(window.show)
# I can pull the settings and pass them on to other windows if needed.
self.setCentralWidget(central)
MainWindow 类选项 2:
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.other_settings = {}
button = QPushButton(\'Show window\')
button.clicked.connect(self.show_other)
def show_other(self):
other_window = OtherWindow()
if other_window.exec():
self.other_settings.update(other_window.settings)
其他窗口类:
class OtherWindow(QDialog):
def __init__(self):
super().__init__()
self.settings = {}
# widgets
box = QSpinBox(objectName=\'spinbox\')
box.valueChanged.connect(self.save_settings)
# and so on ...
def save_settings(self):
sender = self.sender()
self.settings[sender.objectName()] = sender.value()
-
如果设置应该在程序的生命周期内共享和一致,一个解决方案是将其作为主窗口的实例属性,并在创建对话框时将其作为参数传递。
标签: python-3.x qt pyside6