Results 1 to 4 of 4

Thread: Pyqt: How to show a QFileDialog?

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Pyqt: How to show a QFileDialog?

    Hi!

    I would like to choose a path for a file that will be downloaded from internet. To do that I tried to create a button, when pressed will open a QFileDialog so I can choose the path. To make my initial GUI i'm using Qt Designer 5.7 and pyqt5.

    The output when i hit "Folder" button:
    Traceback (most recent call last):
    File "teste.py", line 49, in destination
    self.folderPath = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select folder');
    TypeError: getExistingDirectory(parent: QWidget = None, caption: str = '', directory: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = QFileDialog.ShowDirsOnly): argument 1 has unexpected type 'Ui_MainWindow'
    Aborted (core dumped)
    Qt Code:
    1. from PyQt5 import QtCore, QtGui, QtWidgets
    2. #...
    3. self.destinoButton.clicked.connect(self.destination)
    4. #...
    5. def destination( self ):
    6. self.folderPath = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select folder');
    To copy to clipboard, switch view to plain text mode 

    How can I fix it?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Pyqt: How to show a QFileDialog?

    Well, as Python told you, the first argument of getExistingDirectory() is not what it expects.

    You pass an object of type "Ui_MainWindow" and it expects a QWidget.

    Did you derive from the designer generated class instead of using it?

    Cheers,
    _

  3. #3
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Pyqt: How to show a QFileDialog?

    Here my class definition:
    Qt Code:
    1. from PyQt5 import QtCore, QtGui, QtWidgets
    2.  
    3. class Ui_MainWindow(object):
    4. def setupUi(self, MainWindow):
    5. #...
    6. self.destinoButton.clicked.connect(self.destination)
    7. #...
    8. def destination( self ): #Now instead of "self" is "MainWindow".
    9. self.folderPath = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select folder');
    To copy to clipboard, switch view to plain text mode 

    I changed "self" to "MainWindow" and is working fine. I have no idea why, by is working. I'm newbie in Python.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Pyqt: How to show a QFileDialog?

    This looks like you are changing the code that has been generated from the designer's output instead of just using that code inside MainWindow.

    Which is usually a very bad idea as this means you can no longer use designer for that form, i.e. can't add/change/remove anything with designer, as the next code generation step would then overwrite all your manual code changes.

    I recommend considering to use this properly.

    Cheers,
    _

Similar Threads

  1. QFileDialog to only show executable files in Linux
    By qlands in forum Qt Programming
    Replies: 1
    Last Post: 10th September 2016, 14:47
  2. gif files don't show after compiling with py2exe (using PyQt)
    By illuminatedwax in forum Installation and Deployment
    Replies: 4
    Last Post: 31st January 2011, 04:13
  3. PyQt: How to manipulate QFileDialog
    By xaverxn in forum Newbie
    Replies: 1
    Last Post: 28th October 2010, 20:58
  4. Replies: 5
    Last Post: 6th September 2009, 16:32
  5. QFileDialog::getSaveFileName doesn't show file name.
    By cydside in forum Qt Programming
    Replies: 3
    Last Post: 23rd July 2009, 10:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.