Qt Designer in QGIS

I want the toolbox to prompt out a file select window so that user can select the file and get the path to be used in my Plugin.

Where do I start to code since I just have:

def run(self):
    """Run method that performs all the real work"""

    # Create the dialog with elements (after translation) and keep reference
    # Only create GUI ONCE in callback, so that it will only load when the plugin is started
    if self.first_start == True:
        self.first_start = False
        self.dlg = PalmCounterV2Dialog()

    # show the dialog
    # Run the dialog event loop
    result = self.dlg.exec_()
    # See if OK was pressed
    if result:
        # Do something useful here - delete the line containing pass and
        # substitute with your code.

In your dialog class (PalmCounterV2Dialog) you should add an action to the button with "..." in __init__. And write the method to handle the event see "select_file".

class PalmCounterV2Dialog(...)
    def __init__(...):
        # file_select is the name of the button in Qt designer

    def select_file(self):
        qfd = QtWidgets.QFileDialog()
        filt = self.plugin.tr("Text files(*.txt)") # change the file type
        title = self.plugin.tr("Select a text file")
        f, _ = QtWidgets.QFileDialog.getOpenFileName(qfd, title, ".", filt)
        # finaly put f into the text box
        self.file_name.setText(f) # file_name is the name of your text box
  • Thanks! I've done it!! Nov 17 '20 at 3:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.