You could put those QLabels in a list e.g.:<div><br></div><div>labels = []</div><div>for i in range(3):<br> label = QLabel('%d' % i)<br> label.show()</div><div> labels.append(label) # This line prevents the garbage collection of label after each iteration, since there's a reference to it in this list</div>
<div><br></div><div>A second option could be to add such a QLabel to a layout. This also means the layout will store a reference to this label, preventing it from being deleted automatically.</div><div><br></div><div>Nick<br>
<br><div class="gmail_quote">On Tue, Jun 29, 2010 at 11:01 AM, "V. Armando Solé" <span dir="ltr"><<a href="mailto:sole@esrf.fr">sole@esrf.fr</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">victor wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
hello list,<br>
<br>
i have encountered an issue that i have reduced to this sample code:<br>
<br>
if __name__ == '__main__':<br>
import sys<br>
from PyQt4.QtGui import QLabel<br>
from PyQt4.QtCore import QTimer<br>
from PyQt4.QtGui import QApplication<br>
app = QApplication(sys.argv)<br>
for i in range(3):<br>
l = QLabel('%d' % i)<br>
l.show()<br>
QTimer.singleShot(5000, app.quit)<br>
app.exec_()<br>
<br>
because i'm using the same variable to reference the instances, only one instance remains (the last one) which is not really what i expect.<br>
<br>
what is the way to create several widgets in a loop? thanks.<br>
<br>
</blockquote></div></div>
As with any python object, you have to keep at least a reference to each of the generated objects in order to prevent garbage collection.<br><font color="#888888">
<br>
Armando</font><div><div></div><div class="h5"><br>
<br>
_______________________________________________<br>
PyQt mailing list <a href="mailto:PyQt@riverbankcomputing.com" target="_blank">PyQt@riverbankcomputing.com</a><br>
<a href="http://www.riverbankcomputing.com/mailman/listinfo/pyqt" target="_blank">http://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Nick Gaens<br>
</div>