mwormser mwormser - 5 months ago 35
Python Question

Pressing button in QFileDialog popup exits application

I made the transition from

PyQt4
to
PyQt5
. My app (created with QtDesigner) has a checkbox which enables a "Save" button, in case you want to save your file. In
PyQt4
the dialog would open, I'd choose my file, press OK, done. I implemented a check on the OK button of the main application that would prompt an error if the path was invalid, e.g. if you pressed cancel in the
QFileDialog
.

With
PyQt5
my application exits completely if I close the
QFileDialog
in any way (OK, cancel, X). I want just the QFileDialog to close and not my main window. How do I do this? Thanks for your time and help.

Here's the relevant part of my code:

self.path = self.ui.savepathButton.pressed.connect(lambda: self.file_save())

def file_save(self):
path = QFileDialog.getSaveFileName(self, "Choose a path and filename", os.getcwd().replace("\\", "/") +
"/test.stl", filter="Stereolithography Files (*.stl)")
self.ui.savepath_label.setText(path) <------ NO ERROR WITHOUT THIS LINE

def OKButton_click(self):
if os.path.isdir(os.path.split(self.ui.savepath_label.text())[0]) is False:
# Warning if the filename is invalid.
file_error = QMessageBox()
file_error.setIcon(QMessageBox.Warning)
file_error.setText("Invalid path or filename.")
file_error.setInformativeText("Please choose a working path and filename.") file_error.setWindowTitle("File name error")
file_error.setStandardButtons(QMessageBox.Ok)
file_error.exec_()
else:
self.accept()


Edit:



I know where my error is located, but I still cannot fix it. I marked the line in the code. Why does
self.ui.savepath_label.setText(path)
terminate my application?

Answer

I finally found the (very small) error:

While PyQt4 apparently writes the path automatically as string, PyQt5 does not.

I changed

self.ui.savepath_label.setText(path)

into

self.ui.savepath_label.setText(str(path))

and all is good now.

Comments