0

我试图弄清楚如何将 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
4

1 回答 1

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_())
于 2018-07-09T20:19:07.250 回答