简体   繁体   中英

How to get the path to a file by using the QFileDialog.getOpenFileName() function?

I am trying to display the name of a user-selected file in a line-edit box (for a QGIS python plugin), but nothing happens when I run the code.

Here is my code:

from PyQt5.QtWidgets import QFileDialog

filename = QFileDialog.getOpenFileName(self, 'Open file')[0]
self.dlg.lineEdit.setText(filename)

I hope that somebody can help me

Here is an exemple of code that is working for me, think about testing your resulting path

fpath, filter = QFileDialog.getOpenFileName(None, "Open project", proj_dir, "XML data files (*.xml)")
if len(fpath) > 0:
    do_stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM