【问题标题】:How to pass variables from one QWizardPage to main QWizard如何将变量从一个 QWizardPage 传递到主 QWizard
【发布时间】:2018-12-17 13:50:26
【问题描述】:

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

【问题讨论】:

    标签: python-3.x pyqt5 wizard qwizard


    【解决方案1】:

    如果您想从QWizardPage 访问QWizard,则必须使用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_())
    

    【讨论】:

    • 非常感谢!我从来不知道有一个 Wizard() 方法可以返回与页面相关的向导实例。还要感谢您的额外建议,但即使在按“取消”或“X”而不是“完成”后,它似乎仍然返回值
    • 我刚刚找到了触发事件的解决方案。我没有使用self.finished.connect(self.onFinished),而是使用:self.button(QtWidgets.QWizard.FinishButton).clicked.connect(self.closeEvent),它仅在按下完成按钮时触发操作
    • @Moreorem 不要使用 closeEvent,这是一种不好的做法,请使用 onFinished。
    • 对不起,打错了,我用了onFinished
    • @Moreorem 然后在你对问题的回答中更正它:stackoverflow.com/questions/51233502/…
    猜你喜欢
    • 2016-12-30
    • 1970-01-01
    • 2021-06-15
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    相关资源
    最近更新 更多