python-3.xpyqt5qfiledialog

PyQt5 Retrieve Folder Directory and Set It in lineEdit


Need some advice on how to retrieve the directory path for a selected folder and set it on the LineEdit.

I have the following simple GUI enter image description here

If I clicked the toolButton (the button inside the red-circle), then a dialog window would pop up. Then we could navigate to select the desired folder. I wish that I could pass the path (in string) to the selected folder to the lineEdit box next to the button, once the user clicks Select Folder. However, I could not figure out how to do that. So far here is my code:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_TestQFileDialog(object):
    def _open_file_dialog(self): # a function to open the dialog window
        result = str(QtWidgets.QFileDialog.getExistingDirectory())
        print(result)
        return result

    def setupUi(self, TestQFileDialog):
        TestQFileDialog.setObjectName("TestQFileDialog")
        TestQFileDialog.resize(240, 320)

        self.toolButtonOpenDialog = QtWidgets.QToolButton(TestQFileDialog)
        self.toolButtonOpenDialog.setGeometry(QtCore.QRect(210, 10, 25, 19))
        self.toolButtonOpenDialog.setObjectName("toolButtonOpenDialog")
        directory = self.toolButtonOpenDialog.clicked.connect(self._open_file_dialog)

        self.lineEdit = QtWidgets.QLineEdit(TestQFileDialog)
        self.lineEdit.setEnabled(False)
        self.lineEdit.setGeometry(QtCore.QRect(10, 10, 191, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit.setText('{}'.format(directory))

        self.retranslateUi(TestQFileDialog)
        QtCore.QMetaObject.connectSlotsByName(TestQFileDialog)

    def retranslateUi(self, TestQFileDialog):
        _translate = QtCore.QCoreApplication.translate
        TestQFileDialog.setWindowTitle(_translate("TestQFileDialog", "Dialog"))
        self.toolButtonOpenDialog.setText(_translate("TestQFileDialog", "..."))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    TestQFileDialog = QtWidgets.QDialog()
    ui = Ui_TestQFileDialog()
    ui.setupUi(TestQFileDialog)
    TestQFileDialog.show()
    sys.exit(app.exec_())

Tried to include print in the _open_file_dialog function, and it printed the directory path. However, it was not returned and kept in the directory variable.

Any advice will be much appreciated.

Thanks and regards,

Arnold


Solution

  • Found the answer, the .setText method should be included in the _open_file_dialog function.

    Therefore, the final code would look like this:

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Ui_TestQFileDialog(object):
        def _open_file_dialog(self):
            directory = str(QtWidgets.QFileDialog.getExistingDirectory())
            self.lineEdit.setText('{}'.format(directory))
    
        def _set_text(self, text):
            return text
    
        def setupUi(self, TestQFileDialog):
            TestQFileDialog.setObjectName("TestQFileDialog")
            TestQFileDialog.resize(240, 320)
    
            self.toolButtonOpenDialog = QtWidgets.QToolButton(TestQFileDialog)
            self.toolButtonOpenDialog.setGeometry(QtCore.QRect(210, 10, 25, 19))
            self.toolButtonOpenDialog.setObjectName("toolButtonOpenDialog")
            self.toolButtonOpenDialog.clicked.connect(self._open_file_dialog)
    
            self.lineEdit = QtWidgets.QLineEdit(TestQFileDialog)
            self.lineEdit.setEnabled(False)
            self.lineEdit.setGeometry(QtCore.QRect(10, 10, 191, 20))
            self.lineEdit.setObjectName("lineEdit")
    
            self.retranslateUi(TestQFileDialog)
            QtCore.QMetaObject.connectSlotsByName(TestQFileDialog)
    
        def retranslateUi(self, TestQFileDialog):
            _translate = QtCore.QCoreApplication.translate
            TestQFileDialog.setWindowTitle(_translate("TestQFileDialog", "Dialog"))
            self.toolButtonOpenDialog.setText(_translate("TestQFileDialog", "..."))
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        TestQFileDialog = QtWidgets.QDialog()
        ui = Ui_TestQFileDialog()
        ui.setupUi(TestQFileDialog)
        TestQFileDialog.show()
        sys.exit(app.exec_())