0

I am having browse button in the UI on clicking that should trigger opening filedialog. My issue is opening the filedialog is triggering even before the browse button is clicked. Below is my code

class GisedifySupportDialog(QtWidgets.QDialog, FORM_CLASS):
def __init__(self, parent=None):
    """Constructor."""
    super(GisedifySupportDialog, self).__init__(parent)
    self.setupUi(self)
    self.img_upload=Upload_Image()
    self.img_upload.setupUi(self.upload_image_dialog)
    self.img_upload.pushButton.clicked.connect(self.browseTheFileAudio(self.img_upload.lineEdit))
def browseTheFileAudio(self,lineedit_name):
    self.fileName = QtWidgets.QFileDialog.getOpenFileName(self, "Browse for the file", os.getenv("HOME"))
    self.fileName=self.fileName
    lineedit_name.setText(str(self.fileName))
    return self.fileName

Any reason why the briwseTheFileAudio function is trigerring even before the pushbutton is clicked?

User123
  • 793
  • 4
  • 10
  • 28

1 Answers1

1

When you say:

self.img_upload.pushButton.clicked.connect(self.browseTheFileAudio(self.img_upload.lineEdit))

You are calling the function browseTheFileAudio, and the return value of that function is passed to pushButton.clicked.connect. That's not what you want. You want to pass the function object - without actually invoking it - to pushButton.clicked.connect, which you want to trigger only when the button is clicked. That's how you bind a callback.

Seeing as how your callback also needs a parameter, you can use a lambda:

self.img_upload.pushButton.clicked.connect(lambda le=self.img_upload.lineEdit: self.browseTheFileAudio(le))
Paul M.
  • 10,481
  • 2
  • 9
  • 15
  • Any suggestion on how to access the value passed to the lambda le? Because i am getting a boolean value for le – User123 May 26 '20 at 05:59
  • @User123 If `le` is a boolean, that means that `self.img_upload.lineEdit` is a boolean. Verify that you aren't accidentally replacing that widget reference with a boolean somewhere else in your code. – Paul M. May 26 '20 at 06:01