Destroying instances with PyQT

Boudewijn Rempt boud at valdyas.org
Tue Apr 23 15:56:52 EDT 2002


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()


-- 
Boudewijn Rempt | http://www.valdyas.org



More information about the Python-list mailing list