[PyQt] problem with qtimer

Benno Dielmann lists at benno-dielmann.de
Fri Dec 14 08:38:13 GMT 2007


This is because you don't keep a reference to the dialogs. As soon as method 
newJrnl() terminates, the newly created Jrnls get garbage collected. Your 
snippet works when keeping a reference to the Jrnls, e.g. in a static 
variable (by the way: I highly recommend Mark Summerfield's book "Rapid GUI 
Programming with Python and Qt" - it's all in there: QTimer usage, handling 
of references to Objects, ...):

#!/usr/bin/env python

import sys
from PyQt4 import QtGui, QtCore

class Jrnl(QtGui.QDialog):
    jrnls = set()
    def __init__(self, parent = None):
        QtGui.QDialog.__init__(self, parent)

        self.createFe()
        self.showFe()
    def createFe(self):
        # create widgets, layout, connections, etc
        pass
    def showFe(self):
        self.show()
        QtCore.QTimer.singleShot(1000, self.newJrnl)

    def newJrnl(self):
        if len(Jrnl.jrnls) < 4:
            Jrnl.jrnls.add(Jrnl())

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    jrnl = Jrnl()
    sys.exit(app.exec_())


On Friday 14 December 2007 Pradnyesh Sawant wrote:
| On 20:16, 13 Dec, Benno Dielmann wrote:
| > How about
| >
| > Qtimer.singleShot(1000, self.newJrnl)
| >
| > instead of sleep(1)?
|
| Thanks for the reply; it worked as I wanted.
|
| However there is one small glitch: I get multiple dialog boxes, as I
| require, when I single step through the app. But if I run the app at a
| single shot from the prompt, then only the 1st dialog box comes up (i.e.
| the rest never come up not matter how long I wait), and the whole app
| finishes if I close this 1 dialog box.




More information about the PyQt mailing list