Destroying instances with PyQT
Garry Taylor
gtaylor at lowebroadway.com
Tue Apr 30 05:10:31 EDT 2002
Boudewijn Rempt <boud at valdyas.org> wrote in message news:<3cc59504$0$41709$e4fe514c at dreader4.news.xs4all.nl>...
> Garry Taylor wrote:
>
> > I have attached a QTimer to one of my objects, but I cannot seem to
> > get rid of it when I'm finished, which cause the whole object not to
> > be destroyed, normally my instances are erased simply by erasing the
> > Dictionary in which they reside, which has always worked before the
> > QTimer was added. In the QT manual it says you can destruct an
> > instance like this:
> >
> > QTimer::~QTimer ()
> >
> > How can I translate this into Python?
> >
>
> Simple remove the reference to the QObject that is the parent
> of the QTimer. The following code removes the owning object
> after ten ticks:
>
>
> from qt import *
> import sys
>
> class Q(QObject):
>
> def __init__(self):
> QObject.__init__(self)
> self.o=O(self)
>
> def delete(self):
> self.o = None
>
> class O(QObject):
>
> def __init__(self, parent):
> QObject.__init__(self)
> self.parent = parent
> self.i = 0
> self.t = QTimer(self)
> QObject.connect(self.t, SIGNAL("timeout()"), self.p)
> self.t.start(0, 0)
>
> def p(self):
> self.i += 1
> print "bla", self.i
> if self.i == 10:
> self.parent.delete()
>
> q = Q()
> app=QApplication(sys.argv)
> app.exec_loop()
Thanks, that works fine, but it's not any different to what I was
doing, except that I had sub-classed the QTimer. Anyway, problem
solved.
Thanks
Garry
More information about the Python-list
mailing list