[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyQt] widgets and references in python
From:       Nick Gaens <mail () nickgaens ! com>
Date:       2010-06-29 11:40:22
Message-ID: AANLkTildWdvJ49KcCGd4XzZmsVd9l-4Df0hQLXNax8zn () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


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é" <sole@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

[Attachment #5 (text/html)]

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(&#39;%d&#39; % i)<br>    \
label.show()</div><div>    labels.append(label) # This line prevents the garbage \
collection of label after each iteration, since there&#39;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, &quot;V. Armando \
Solé&quot; <span dir="ltr">&lt;<a \
href="mailto:sole@esrf.fr">sole@esrf.fr</a>&gt;</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__ == &#39;__main__&#39;:<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(&#39;%d&#39; % i)<br>
        l.show()<br>
    QTimer.singleShot(5000, app.quit)<br>
    app.exec_()<br>
<br>
because i&#39;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>



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

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic