Put a button in a GUI in order to open a folder not a file
-
Hello I am trying to put a button icon on a GUI Widget, so that when I press it to let me select a folder not a file. After google searching I found this: https://stackoverflow.com/questions/41587541/pyqt-assign-to-a-button-the-ability-to-choose-a-directory So, my try is this:
dialog = QFileDialog() file = dialog.getExistingDirectory(None, "Select Folder") self.tb.addWidget(dialog)
The problem is that when I execute the code, the first thing I see is a select directory menu, not the GUI on which I need to place the icon button and when I choose folder the code collapse. When I remove the above code, everything works fine.... Do you see something strange on my code?
-
@john_hobbyist
You need to write this in the slot attached to the button'sclicked()
signal. -
@john_hobbyist said in Put a button in a GUI in order to open a folder not a file:
file = dialog.getExistingDirectory(None, "Select Folder")
Ok, I have build a QPushButton that when pressed it calls do_something() method. What do I put inside the method so that I can choose a directory and not a file...??
Because when I tried file = dialog.getExistingDirectory(None, "Select Folder") with QPushButton I got this error:
AttributeError: 'QPushButton' object has no attribute 'getExistingDirectory' -
@john_hobbyist said in Put a button in a GUI in order to open a folder not a file:
file = dialog.getExistingDirectory(None, "Select Folder")
AttributeError: 'QPushButton' object has no attribute 'getExistingDirectory'Then your
dialog
variable must have been aQPushButton
.... And that is not what you show with your earlierdialog = QFileDialog()
. -
@john_hobbyist apparently, you named your QPushButton
dialog
.getExistingDirectory is a static function, you can simply call it this way:
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "/home", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
https://doc.qt.io/qt-6/qfiledialog.html#getExistingDirectory
-
-
@john_hobbyist
But you had something which worked for this originally:file = dialog.getExistingDirectory(None, "Select Folder")
. Or you might change it to:dirName = QFileDialog.getExistingDirectory(self, "Select Folder")
You don't need or use the
dialog = QFileDialog()
for callinggetExistingDirectory()
because it is a static method ofQFileDialog
. -
@john_hobbyist moved this to Qt for Python subform, since it is the python part you're having problems with :D