PyQt4 File select widget

南笙酒味 提交于 2019-12-03 04:52:33

There is no file dialog available from the Qt designer as far as I know. But you can easily do it with a few lines of code.

Assuming you have a simple button called pushButton and the path should be stored in lineEdit.

def selectFile():
    lineEdit.setText(QFileDialog.getOpenFileName())

pushButton.clicked.connect(selectFile)

[edit]Just wondering though, are you using KDE by any chance? If so, than you can use the KUrlRequester for this. It can easily be configured to support anything from files to urls to directories.

Vicky T

QFileDialog exists in QtGui. At least in my version 4.4 and probably much earlier too. I think the reason it is not in Designer is because it opens its own window instead of being a widget to place on another window.

The documentation from QTDesigner could be better and at least hint of its existence.

Instantiate it and run the show command. It comes right up and defaults to /.

import QtGui
self.fileDialog = QtGui.QFileDialog(self)
self.fileDialog.show()

You can use method getOpenFileName() in QFileDialog Class.

QFileDialog.getOpenFileName() will return the file path and the selected file type

I got this : ('C:/Users/Sathsara/Desktop/UI/Test/test.py', 'All Files (*)')

To get only the file path use QFileDialog.getOpenFileName()[0]


Sample code:

def selectFile():
   print(QFileDialog.getOpenFileName()[0])


dlg.locationBtn.clicked.connect(selectFile)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!