You could put those QLabels in a list e.g.:

labels = []
for i in range(3):
    label = QLabel('%d' % i)
    label.show()
    labels.append(label) # This line prevents the garbage collection of
label after each iteration, since there's a reference to it in this list

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.

Nick

On Tue, Jun 29, 2010 at 11:01 AM, "V. Armando Solé" <s...@esrf.fr> wrote:

> victor wrote:
>
>> hello list,
>>
>> i have encountered an issue that i have reduced to this sample code:
>>
>> if __name__ == '__main__':
>>    import sys
>>    from PyQt4.QtGui import QLabel
>>    from PyQt4.QtCore import QTimer
>>    from PyQt4.QtGui import QApplication
>>    app = QApplication(sys.argv)
>>    for i in range(3):
>>        l = QLabel('%d' % i)
>>        l.show()
>>    QTimer.singleShot(5000, app.quit)
>>    app.exec_()
>>
>> 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.
>>
>> what is the way to create several widgets in a loop? thanks.
>>
>>  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.
>
> Armando
>
>
> _______________________________________________
> PyQt mailing list    PyQt@riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>



-- 
Nick Gaens
_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to