Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, I'm assuming that the code in your question is an example and not your real code. Because, since you're creating your widgets in a loop with no additional parameters, the result would be a overlap of all the objects in the window. You see, all the lineEdits would be in the same place (by default, the top left part of the window). If that's the case, will be really hard to tell where is the problem since it appears to me that it's just a matter of knowing which instance of the lineEdit is sending the signal.</p> <p>So I will tell you how to make the code above work and if it's not the case you can always reply.</p> <p>It's all about the way the signal is connected. If you take a look at your code, you're creating all the lineEdits with the same name (self.valueField). The objects are all created and they have their own position in the memory, but their reference in the connect is always overriden by the last connect. You can even make the test. If you add this line to your checkState function: </p> <p><code>print self.fieldList.index(line)</code> </p> <p>You'll see that the signal is always called for the last item of the list, which is also the last signal connected.</p> <p>You can simply set all your lineEdits one by one with a different name. And create your list also typing all the names in it. This way your for loop will only connect the lineEdits and not create them anymore.</p> <p>EDIT:</p> <p>Ok. One way to make this work is to use a SignalMapper. Take a look at the <a href="http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qsignalmapper.html" rel="nofollow">docs here</a>.</p> <p>In the example above you can do something like this:</p> <p>first declare the mapper, the same way you declare other widgets:</p> <pre><code>self.myMapper = QtCore.QSignalMapper() </code></pre> <p>After your first for loop. You iterate over the list:</p> <pre><code>for item in self.fieldList: self.myMapper.setMapping(item, self.fieldList.index(item)) item.textEdited.connect(self.myMapper.map) self.myMapper.mapped[int].connect(checkState) </code></pre> <p>So what this will do is that it will map every object in your list with its corresponding index on the list (int). So when a signal is emitted, the mapper calls the checkState function with that index value as argument. This way you can reach your object using the list again:</p> <pre><code>def checkState(i): print (self.fieldList[i].objectName()) self.fieldList[i].setText("It works!") </code></pre> <p>As you can see the mapper will send only the index as argument. So I guess you will need to adapt your function with the additional args <code>palette1, palette2</code>. Or you can use a preliminar function like a filter and then call the right checkState function inside it:</p> <pre><code>def myfilter(i): checkState(self.fieldList[i], palette1, palette2) </code></pre> <p>I'm pretty sure that exist other approaches to make this work, but this one does the job while a better solution doesn't show up.</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