Solved QFileDialog- Using getOpenFile
-
@jsulm I'm not getting any error message
-
@LT-K101 Please run through debugger - this is the first thing to do in such a situation.
From the code I don't see why it would crash, so please use debugger... -
@LT-K101 said in QFileDialog- Using getOpenFile:
if file_name[0].endswith('.pdf'):
Was a file definitely selected? If
file_name
is empty, this line would cause a crash. -
@mchinand Agree.
But only if "my application exits as soon as i click on open file" is not what really happens :-) -
@jsulm
... plus you do/should get a traceback error message from Python ... :) -
@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. -
@artwaw
Yes I don't think it matters but since half the point ofwith open
is that it closes the file for you it seems best not toclose
it. Though as I said I don't think it's getting that far here. -
@jsulm 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. This is really strange.
-
-
@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 You don't check whether user cancelled the dialog.
-
@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. -
@jsulm I just did try to cancel and nothing happened, everything works fine. How to save the selected image file in database and display with the other text on the window is what i'm reading on now. Please any assistance?
-
@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? -
@jsulm Yes I'm using SQL.
-
@LT-K101 Start here: https://doc.qt.io/qt-5/qtsql-index.html
-
@JonB Oh! my bad. I meant when user clicks on the cancel the dialog window exits.
-
@LT-K101
Now I'm really lost. From theQFileDialog
clicking either Cancel or selecting a file and clicking OK should close/exit that dialog window. Originally you saidbut my application exits as soon as i click on open file
If you want help can you please state clearly whether this is still the case and/or when your application exits upon closing the file dialog.