【问题标题】:Proper way of managing multiple windows in PySide?在 PySide 中管理多个窗口的正确方法?
【发布时间】: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


【解决方案1】:

如果我正确理解了您的问题,请查看可能有用的link

我的小补充是look here

from PyQt5.QtWidgets import QDialog
from ui_imagedialog import Ui_ImageDialog

class ImageDialog(QDialog):
    def __init__(self):
        super(ImageDialog, self).__init__()

        # Set up the user interface from Designer.
        self.ui = Ui_ImageDialog()
        self.ui.setupUi(self)

        # Make some local modifications.
        self.ui.colorDepthCombo.addItem("2 colors (1 bit per pixel)")

        # Connect up the buttons.
        self.ui.okButton.clicked.connect(self.accept)
        self.ui.cancelButton.clicked.connect(self.reject)

获得 .ui 文件后,使用它是一个好习惯(第二个链接,第二个示例 - 上面提到过)pyside6-uic或者pyuic5- 取决于您的需求。

然后你正在创建类,它的作用是设置所需的 QWidget ui 转换 python 模块。

所以在 QtDesigner 中你正在创建一个 QWidget: screen_qt_designer

然后做你想做的所有事情——放在 QLineEdit、QPushButtons 等上,然后将该文件保存在项目的文件夹中。之后你所要做的就是使用皮尤克或者pyside6-uic使用适当的设置(查看 --help 以设置输出文件名)。 然后,您所拥有的只是一个 .ui 文件(如果您想添加一些小部件或全部更改小部件,则使用该文件)和您生成的继承 QObject 的 python 类,例如:

class Ui_Form(object):
    def setupUi(self, Form):
        if not Form.objectName():
            Form.setObjectName(u"Form")
        Form.resize(800, 600)
        self.verticalLayout = QVBoxLayout(Form)
        self.verticalLayout.setObjectName(u"verticalLayout")
        self.label = QLabel(Form)
        self.label.setObjectName(u"label")

然后创建另一个 Python 文件,继承这个生成的类,例如:

from PySide6.QtWidgets import QWidget
from CreateNewDatabase_ui import Ui_Form

class NewDatabaseWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)

就这样。您已经准备好 QWidget 以将其添加到 QStackedWidget 中。我在自己的程序中使用它,它非常适合我。

【讨论】: