2

I use to write QGis plugins, which is based on PyQT. I use to create QButtons and connect their click to a function that itself calls a QFileDialog.

For example:

def doOpenFile(self):
        fname = QFileDialog.getOpenFileName(self.dlg, self.tr("Open input file") )
        if fname:
            self.dlg.editInFile.setText(fname)

    # ____________
    def doInitGUI(self):
        # connect buttons
        self.dlg.buttonInFile.clicked.connect( self.doOpenFile )

So: I click on a button, which triggers a QFileDialog interface. Quite ofter (but not always, and I could not define the conditions), the QFileDialog will re-open itself after clicking on "OK" or "Cancel". It requires me to cancel several times to stop the QFileDialog re-opening. I suppose that the click event is thrown several times, is it correct? How can I solve this behavior?

Bruno von Paris
  • 871
  • 11
  • 24

2 Answers2

1

Maybe try to use getOpenFileName without these parameters.

I've done this before with:

dialog = QtGui.QFileDialog()
fname = dialog.getOpenFileName(None, "Import JSON", "", "JSON files (*.json)")

and I've never had any problems like yours.

dmh126
  • 6,732
  • 2
  • 21
  • 36
1

My problem came from the fact that I was calling self.doInitGUI from the run() method of my plugin, therefore the connect function was called every time I ran my code. I moved the initialisation of the connections (the call to self.doInitGui) at the end of the add_action() method, which is called only one time, and I do not have this problem anymore.

Bruno von Paris
  • 871
  • 11
  • 24