Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing list elements as signal/slot arguments in PyQt
    text
    copied!<p>I want to pass object which calls on signal, as an argument to function so I can manipulate with that object in that function. That object is QLineEdit widget. Here is the example:</p> <pre><code>self.fieldList = [] for i in range(10): self.valueField = QtGui.QLineEdit() self.fieldList.append(self.valueField) self.fieldList[i].cursorPositionChanged.connect(lambda: (self.checkState(self.fieldList[i], palette1, palette2))) def checkState(self, line, palette1, palette2): if len(line.text()) &gt; 3: line.setPalette(palette1) else: line.setPalette(palette2) </code></pre> <p>So, as you can see, I'm trying to pass list element <em>self.fieldList[i]</em> as an argument with name <em>line</em> in <em>checkState()</em> function. If I explicitly define <em>fieldList</em> element (i.e. <em>self.fieldList[0]</em>) in the <em>checkState()</em> function, the code works perfectly, but I can't pass it as argument. What am I doing wrong?</p> <p>Btw. compiler is not giving me error message when I try to run the program, but it simply won't do the job it's supposed to (change the color of the QLineEdit when I write more than 3 characters)</p> <p><strong>EDIT:</strong></p> <p><strong>@dex19dt</strong></p> <p>Yes, this is an example, but you are right! Only the last QLineEdit works like it should!</p> <p>The problem is, I can't give all those widgets names because their number depends on the layer selected, which means sometimes there are 5 widgets, sometimes 11 or 75 even.</p> <p>I can easily name those widgets and set text like so:</p> <pre><code> self.fieldList[i].setObjectName(_fromUtf8("attributeValueField_{0}".format(i))) self.fieldList[i].setText(_fromUtf8("{0}".format(value.toString()))) </code></pre> <p>and this works just fine for every widget, but I don't know how to define signals? Do you have an idea?</p> <hr> <p><strong>EDIT:</strong></p> <p><strong>@dex19dt</strong></p> <p>OK, so I asked about this problem of index not "sticking" to the function call, and I found out that this is just the way python works and to fix this problem I needed to replace this line:</p> <pre><code>self.fieldList[i].cursorPositionChanged.connect(lambda: (self.checkState(self.fieldList[i], palette1, palette2))) </code></pre> <p>with this one:</p> <pre><code>self.fieldList[i].cursorPositionChanged.connect(lambda old, new, i=i: (self.checkState(self.fieldList[i], palette1, palette2))) </code></pre> <p>so this way current index copies to the signal index. Although I'm not really sure why is there <code>old, new</code> part. Does it refer to the lambda function or simply defines left <code>i</code> as old and right <code>i</code> as new variable... Tried googling it, but without success. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload