QFileDialog- Using getOpenFile
-
Hi, I'm trying to let user browse and choose pdf files from directory but my application exits as soon as i click on open file instead of displaying the file name in lineEdit. Please i need assistance, Thanks. Below is my code.
self.ui.Second_Browse_Button.clicked.connect(self.Load_First_Certificate) def Load_First_Certificate(self): dialog = QFileDialog() dialog.setFileMode(QFileDialog.AnyFile) dialog.setFilter(QDir.Files) if dialog.exec_(): file_name = dialog.selectedFiles() if file_name[0].endswith('.pdf'): with open(file_name[0], 'r') as f: data = f.read() self.lineEdit_108.setPlainText(data) f.close()
-
@LT-K101 said in QFileDialog- Using getOpenFile:
but my application exits as soon as i click on open file
Did you at least run through debugger to see where exactly it crashes?
And what is the error code/signal? -
@LT-K101 said in QFileDialog- Using getOpenFile:
instead of displaying the file name in lineEdit
In addition to @jsulm. Your code does not attempt to display the selected file name, it attempts to open it and put its content (which will presumably be a large amount of PDF) into the line edit.
Although I do not think it will cause a crash, you should not be calling
f.close()
inside awith open ...
.If for whatever reason your
open()
fails (e.g. non existent file because of yourQFileDialog.AnyFile
) I believe in Python you will get a runtime error, which if you are not trapping would exit the script. You should also chek the length of yourfile_name
before indexing it. -
@JonB said in QFileDialog- Using getOpenFile:
Although I do not think it will cause a crash, you should not be calling f.close() inside a with open ....
with
statement callsclose()
at the end of the statement. callingf.close()
is an obvious error here, however simple calling close() twice on the same object doesn't produce an error. Reading from the closed file does though. -
@LT-K101 said in QFileDialog- Using getOpenFile:
When I click on the Browse Button to open the QDialog window for file selection and i click cancel it freezes the application and exit.
- Use a debugger/
print
statements to see where execution is going. - Are there any further code lines in
def Load_First_Certificate(self):
after what you show? - Is there some other window in the application currently open other than the file chooser dialog when you close it?
- Use a debugger/
-
@JonB I did print in the
def Load_First_Certificate(self):
function and it worked fine. I'm now using the code below to load the file as image.def Load_Second_Certificate(self): fname5, _ = QFileDialog.getOpenFileName(self, "Choose File", "", "All Files(*) ;; Images(*.jpeg)") pixmap = QPixmap(fname5, _) self.ui.Display_label_6.setPixmap(QPixmap(pixmap)) self.ui.Display_label_6.repaint()
-
@LT-K101 said in QFileDialog- Using getOpenFile:
I did print in the def Load_First_Certificate(self): function and it worked fine.
Then you should be able to see how far the code got to in execution when the " application exits as soon as i click on open file".
I suggest you answer my earlier question #3.
You should also make your new code check for cancellation, like your old code did, as @jsulm has remarked.
You have changed your code, and what it does, but said nothing about whatever problem it now has.
You do not check the result from
pixmap = QPixmap(fname5, _)
, and I don't know if that_
is the same variable as on the line above it, which would be wrong if it is. -
@LT-K101 said in QFileDialog- Using getOpenFile:
Please any assistance?
What kind of database? SQL? https://doc.qt.io/qt-5/qtsql-index.html
-
@LT-K101 said in QFileDialog- Using getOpenFile:
@jsulm I just did try to cancel and nothing happened, everything works fine.
That is truly remarkable, with your existing code.
fname5, _ = QFileDialog.getOpenFileName(self, "Choose File", "", "All Files(*) ;; Images(*.jpeg)") pixmap = QPixmap(fname5, _)
So after the user clicks Cancel in the dialog, what is returned in
fname5
and most importantly what in your code stops so that it does not try to load aQPixmap
? Or do you mean it just seems to be OK when you continue ignoring the Cancel and try to load aQPixmap
from "nothing"? Try first running this and picking a suitable file to load, then re-running it and pressing Cancel, that leaves the currently loaded pixmap as-is, does it?