Here is my problem. I want to create n number of QLineEdit widgets (n can be any number, depends on the input values). I can create those QLineEdit widgets, give them object name and text, but I can't define signals on all of them (I want that every QLineEdit widget emits signal when interacting with it). Here is the example od the code:
self.fieldList = {}
for (i, value) in attrMap.iteritems():
self.fieldList["valueField" + str(i)] = QtGui.QLineEdit()
self.fieldList["valueField" + str(i)].setObjectName(_fromUtf8("attributeValueField_{0}".format(i)))
self.fieldList["valueField" + str(i)].setText(_fromUtf8("{0}".format(value.toString())))
self.fieldList["valueField" + str(i)].cursorPositionChanged.connect(lambda: (self.checkState(self.fieldList["valueField" + str(i)])))
The problem persist in the last line:
self.fieldList["valueField" + str(i)].cursorPositionChanged.connect(lambda: (self.checkState(self.fieldList["valueField" + str(i)])))
I thought that signal would stick to every widget, but because of the loop, signal "jumps" from widget to the widget until it gets to the last widget so only that last widget is able to emit signal. So how can I arrange that every widget emits signal when being changed?
Explanation and common solution here (there are many more, just search for e.g. "python lambda loop parameter": https://stackoverflow.com/a/938493
Another common solution is to use a partial function:
import functools
slot = functools.partial( self.checkState, self.fieldList["valueField" + str(i)] )
self.fieldList["valueField" + str(i)].cursorPositionChanged.connect( slot )