I am trying to figure out how to pass variables from (e.g.: an openFile function) inside a QWizardPage class to the main QWizard class. I have also read about signals and slots but can't understand how and if this is the ideal way to do it when using PyQt5. Here follows a simplified example of the code:
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
if you want to access QWizard
from QWizardPage
you must use the wizard()
method, on the other hand closeEvent()
is an event that is triggered when the window is closed that should not be invoked by an additional signal that is not necessary, the correct thing is to create a slot that connects to the finished
signal.
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_())