22

I want to make a QT4 (using QT designer) dialog, that contains a part where a file has to be selected.

Now, I know QFileDialog exists, and I can program something that does what I want. But can I also just do it in QT designer?

Is there some way to get a "file select" widget in QT designer? Or, I remember these buttons, having the selected file as a title and a little arrow allowing the user to select another file by the QFileDialog?

So is there a ready made solution, or do I have to program it myself?

Joe
  • 10,698
  • 7
  • 47
  • 63
Nathan
  • 465
  • 2
  • 5
  • 14

3 Answers3

35

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.

Wolph
  • 69,888
  • 9
  • 125
  • 143
  • hey I have to do same thing but I am getting error as `NameError: global name 'QFileDialog' is not defined` – lkkkk Sep 04 '14 at 07:04
  • 1
    @Latik: you need to import it of course, `from PyQt4.QtGui import QFileDialog` – Wolph Sep 04 '14 at 07:32
  • KUrlRequester is really nifty, but will it work (be readily available) on Windows if I install KDE for Windows or does it only work on \*NIX systems? – Agi Hammerthief Aug 04 '16 at 19:46
  • @AgiHammerthief I suspect it won't work without compiling manually so I personally wouldn't recommend on trying it. On Windows it's probably easier to simply open the native file dialog – Wolph Aug 06 '16 at 21:27
  • 1
    in PyQT5, import will be as the following: from PyQt5.QtWidgets import QFileDialog – Bassem Shahin Mar 31 '18 at 18:00
  • 1
    In PySide, there is the same function (getOpenFileName) but it returns a tuple, not a string. So I should use the index like getOpenFileName()[0] . I don't know how it would be in PyQt. – Kim Jun 21 '18 at 04:14
  • @Kim I had to to use `QFileDialog.getOpenFileName()[0]` as well, and it works just fine. – Jacques Mathieu Jun 26 '19 at 23:57
13

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()
Aditya
  • 369
  • 6
  • 20
Vicky T
  • 1,643
  • 4
  • 15
  • 12
4

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)