如何将变量从一个QWizardPage传递到主QWizard

时间:2018-07-09 19:45:37

标签: python-3.x pyqt5 wizard qwizard

我试图弄清楚如何将QWizardPage类内部的变量(例如:openFile函数)传递给主QWizard类。我还阅读了有关信号和插槽的信息,但不了解使用PyQt5的方式和方法是否是理想的方式。 以下是该代码的简化示例:

class ImportWizard(QtWidgets.QWizard):
  def __init__(self, parent=None):
    super(ImportWizard, self).__init__(parent)
    self.addPage(Page1(self))
    self.setWindowTitle("Import Wizard")
    # Trigger close event when pressing Finish button to redirect variables to backend
    self.finished.connect(self.closeEvent)

  def closeEvent(self):
    print("Finish")
    # Return variables to use in main
    print(self.variable)


class Page1(QtWidgets.QWizardPage):

  def __init__(self, parent=None):
    super(Page1, self).__init__(parent)
    self.openFileBtn = QPushButton("Import Edge List")
    layout = QtWidgets.QVBoxLayout()
    layout.addWidget(self.comboBox)
    layout.addWidget(self.openFileBtn)
    self.setLayout(layout)
    self.openFileBtn.clicked.connect(self.openFileNameDialog)

  def openFileNameDialog(self, parent):
    options = QFileDialog.Options()
    options |= QFileDialog.DontUseNativeDialog
    fileName, _ = QFileDialog.getOpenFileName(
        self, "QFileDialog.getOpenFileName()", "",
        "All Files (*);;Python Files (*.py)", options=options)
    # if user selected a file store its path to a variable
    if fileName:
        self.parent.variable = fileName

1 个答案:

答案 0 :(得分:0)

如果您想从QWizard访问QWizardPage,则必须使用wizard()方法,另一方面,closeEvent()是在关闭窗口时触发的事件不应被不必要的附加信号调用,正确的做法是创建一个连接到finished信号的插槽。

from PyQt5 import QtCore, QtWidgets

class ImportWizard(QtWidgets.QWizard):
    def __init__(self, parent=None):
        super(ImportWizard, self).__init__(parent)
        self.addPage(Page1(self))
        self.setWindowTitle("Import Wizard")
        # Trigger close event when pressing Finish button to redirect variables to backend
        self.finished.connect(self.onFinished)

    @QtCore.pyqtSlot()
    def onFinished(self):
        print("Finish")
        # Return variables to use in main
        print(self.variable)

class Page1(QtWidgets.QWizardPage):

    def __init__(self, parent=None):
        super(Page1, self).__init__(parent)
        self.openFileBtn = QtWidgets.QPushButton("Import Edge List")
        self.comboBox = QtWidgets.QComboBox()
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.comboBox)
        layout.addWidget(self.openFileBtn)
        self.setLayout(layout)
        self.openFileBtn.clicked.connect(self.openFileNameDialog)


    @QtCore.pyqtSlot()
    def openFileNameDialog(self):
        options = QtWidgets.QFileDialog.Options()
        options |= QtWidgets.QFileDialog.DontUseNativeDialog
        fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
            self, "QFileDialog.getOpenFileName()", "",
            "All Files (*);;Python Files (*.py)", options=options)
        # if user selected a file store its path to a variable
        if fileName:
            self.wizard().variable = fileName


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = ImportWizard()
    w.show()
    sys.exit(app.exec_())