Reputation: 59
Sorry the title is such a mouth full, but basically what I am trying to do here is that I have 3 buttons, all of them open the QFileDialog for the user to choose a file to use. Since the action is the same, I would like to use the same function for all 3, but depending on which button is pressed, I need to update different QLineEdit to reflect the file path in the GuI. How do I go about achieving that?
#Create the prompt for user to load in the q script to use
self.qFileTF = QtGui.QLineEdit("Choose the q script file to use")
self.qFileButton = QtGui.QPushButton("Open")
self.qFileButton.setFixedSize(100,27)
self.fileLayout1.addWidget(self.qFileTF)
self.fileLayout1.addWidget(self.qFileButton)
#Create the prompt for user to load in the light house file to use
self.lhFileTF = QtGui.QLineEdit("Choose the light house file to use")
self.lhButton = QtGui.QPushButton("Open")
self.lhButton.setFixedSize(100,27)
self.fileLayout2.addWidget(self.lhFileTF)
self.fileLayout2.addWidget(self.lhButton)
#Create the prompt for user to choose to reference an older version of q script
self.oldQCB = QtGui.QCheckBox("Reference an older version Q script")
self.oldQTF = QtGui.QLineEdit("Choose the q script file to use")
self.oldQTF.setEnabled(False)
self.oldQButton = QtGui.QPushButton("Open")
self.oldQButton.setEnabled(False)
self.oldQButton.setFixedSize(100,27)
self.fileLayout3.addWidget(self.oldQTF)
self.fileLayout3.addWidget(self.oldQButton)
self.connect(self.qFileButton, QtCore.SIGNAL("clicked()"), self.loadFile)
self.connect(self.lhButton, QtCore.SIGNAL("clicked()"), self.loadFile)
self.connect(self.oldQButton, QtCore.SIGNAL("clicked()"), self.loadFile)
def loadFile(self):
selFile = QtGui.QFileDialog.getOpenFileName()
if self.qFileButton:
self.qFileTF.setText(selFile)
elif self.lhFileTF:
self.lhFileTF.setText(selFile)
else:
self.oldQTF.setText(selFile)
Upvotes: 1
Views: 303
Reputation: 29896
You can pass the QLineEdit
to change as an additionnal parameter to the slot using a lambda function (or a partial function):
def loadFile(lineEdit):
return lambda: self.loadFile(lineEdit)
self.qFileButton.clicked.connect(loadFile(self.qFileTF))
self.lhButton.clicked.connect(loadFile(self.lhFileTF))
self.oldQButton.clicked.connect(loadFile(self.oldQTF))
def loadFile(self, lineEdit):
selFile = QtGui.QFileDialog.getOpenFileName()
lineEdit.setText(selFile)
Upvotes: 1
Reputation: 20359
Try to use the sender
method:
def loadFile(self):
selFile = QtGui.QFileDialog.getOpenFileName()
if self.sender() == self.qFileButton:
self.qFileTF.setText(selFile)
elif self.sender() == self.lhFileTF:
self.lhFileTF.setText(selFile)
else:
self.oldQTF.setText(selFile)
Upvotes: 2