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.