Solved QFileDialog- Using getOpenFile
-
@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.
-
@JonB @JonB Any help on how to display only file name instead of displaying file full path using the QFileDialog? Thanks in advance. Below is my code.
def Load_First_Certificate(self): self.fname1, ok = QFileDialog.getOpenFileName(self, "Upload Pdf File", "", "Pdf Files(*.pdf)") if ok: defaultPdf = os.path.split(self.fname1) self.ui.Pdf_lineEdit.setText(self.fname1)
-
@LT-K101 If you look at the QString (or str in Python) documentation you will surely notice that there is split() method. So self.fname1.split('/').last() should do the trick. Why don't read documentation on the basic types you use?
-
print(QFile(self.fname1).fileName()) print(QFileInfo(self.fname1).fileName())
Your code:
self.fname1, ok = QFileDialog.getOpenFileName(self, "Upload Pdf File", "", "Pdf Files(*.pdf)") if ok:
Where do you get the idea that the second result is any kind of
ok
parameter, and that you should be looking at it for success? -
self.ui.Pdf_lineEdit.setText(os.path.basename(self.fname1))
-
@jsulm
You are encouraging OP to mix native Python calls with Qt calls when no need to, grrr! SinceQFileDialog.getOpenFileName()
returns a Qt-type-path (right?), he must now go check up howos.path....
handles e.g./
s under Windows.... -
@JonB Sorry that was a function i wrote for my image upload function, if user does not upload an image then the default image should be an avatar. I copied it and forgot to delete the if statement. Thanks
-
@jsulm Thanks it's working! After selecting the pdf file saving to a location in my working directory is the problem now. Any assistance please?
-
@LT-K101 said in QFileDialog- Using getOpenFile:
Any assistance please?
Please ask a concrete question. What is the concrete problem you're having now?
-
@jsulm I want to save the uploaded file itself to a specific directory not just the filename. Example when the user click on open button to select file , after user select file. The file should be saved in a directory.
-
@LT-K101 Then do so: https://doc.qt.io/qt-5/qfile.html
-
@jsulm Thanks