Reputation: 921
I'm new to GUI designing and I started working with QT Designer 4.8.6. I'm connecting buttons to function using the signals-slots mechanism.
Here is some connections generated by the pyuic4 util to create the GUI script:
QtCore.QObject.connect(self.btn_add_codes, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.sel_codes_file)
QtCore.QObject.connect(self.btn_add_xls, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.sel_excel_file)
The associated functions in my main python file (path_codes
and path_excel
are QLineEdit widgets):
class MainDialog(QtGui.QMainWindow, gui_v1.Ui_MainWindow):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
self.setupUi(self)
def sel_codes_file(self):
self.path_codes.setText(QtGui.QFileDialog.getOpenFileName())
def sel_excel_file(self):
self.path_excel.setText(QtGui.QFileDialog.getOpenFileName())
I want to use a generic function for all the buttons which actions is to search for a file and show the path in a LineEdit widget. I added this function to my MainDialog class:
def select_file(self, textbox):
self.textbox.setText(QtGui.QFileDialog.getOpenFileName())
I modified the connection to:
QtCore.QObject.connect(self.btn_add_codes, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.select_file(textbox=self.path_codes)
It's not working. The main window does not show with this code and I'm getting this error: AttributeError: 'MainDialog' object has no attribute 'textbox'
Is it possible to pass arguments to slots connection functions ? If so, what am I doing wrong ? Thanks !
Upvotes: 1
Views: 449
Reputation: 3354
Does this lambda work? At least that's how I do it with Qt when using C++.
self.btn_add_codes.clicked.connect(lambda codes=self.path_codes: MainWindow.select_file(codes))
Upvotes: 1