2

I am new to QGIS and I'm building a plugin on it. The problem I'm facing, I believe, concerns mostly PyQt5.

So, in my plugin I'm trying to open a file through getOpenFileName() of QFileDialog. I noticed that the dialog for opening the file is triggered as many times I trigger the plugin without reloading (using Plugin Reloader). Putting it simply, the dialog for opening the file pops up as many times in a row as I test the plugin without reloading it. The first time user has to input file name once, the second time twice, third time thrice and so on. I want this dialog to pop up just once and get the input from the user.

I'm using Plugin Builder. Here's my code for reference:

def loadData(self):
    """Run method that performs all the real work"""
    # show the dialog
    self.dlg.show()
    # Run the dialog event loop

    self.dlg.lineEdit.clear()
    self.dlg.toolButton.clicked.connect(self.select_output_file)

    result = self.dlg.exec_()
    # See if OK was pressed
    if result:
        # ...

def select_output_file(self):
    filename = QFileDialog.getOpenFileName(self.dlg, "Select file", "")[0]
    self.dlg.lineEdit.setText(self.tr(filename))

Here, lineEdit is the name for text field where the path of the file being opened appears, toolButton is the button that is clicked to trigger this action and dlg is the instance for main dialog box of the plugin.

3

It is likely because each time the plugin is run, you are adding another connect signal to the function. You should disconnect this at the end of your loadDate(self) function using:

self.dlg.toolButton.clicked.disconnect(self.select_output_file)

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.