[PyQt] how to set up a timer to call a method after a delay?

Robin Wittler real at the-real.org
Thu Jul 1 02:17:18 BST 2010

On 07/01/2010 02:53 AM, Steve Castellotti wrote:
> On Wed, 2010-06-30 at 18:19 +0100, Phil Thompson wrote:
>>> self.timer = QtCore.QTimer()
>>> self.timer.connect(self.timer, QtCore.SIGNAL("timeout()"),
>>> self.sendData)
>>> self.timer.start(1000) # 1 second
>>>      ...but received an exception that "QTimer can only be used with
>>> threads started with QThread" which leads me to believe I will need to
>>> implement threading for my server (also I expected to need to send
>>> "parent=self" when instantiating the QTimer object so I know I must be
>>> doing something wrong).
>> QTimer.singleShot(1000, self.sendData)
>> ...is the normal way of doing it.
>      Yes and thank you, that gets me closer to the style I was using
> under Twisted, however, with my code looking like this:
> self.timer = QtCore.QTimer.singleShot(1000, self.sendData)
>      I'm still getting this error:
> QObject::startTimer: QTimer can only be used with threads started with
> QThread
>      Comparing to an example I found online here:
> http://www.rkblog.rk.edu.pl/w/p/qtimer-making-timers-pyqt4/
>      The main difference that I can see is that example is using
> QApplication to start up, whereas I am only running from the console (no
> Gui at this point).
> That said, QApplication inherits from QCoreApplication, which inherits
> from QObject. QThread also inherits form QObject, but there's nothing
> obvious I see that would indicate QApplication inherits from QThread
> directly. Is there some other component related to Gui applications
> which are creating QThreads which I need to replicated?
> I tried setting up my program to inherit from QtCore.QThread just to see
> if that would cover it, but no joy.
> Do I need to re-code my program to open each connection as distinct
> objects in dedicated threads and perform the timers within those
> objects, or again is there something simple and obvious which I am just
> missing?
> Cheers
> Steve Castellotti


take a look at

QApplication.__init__ (self, list-of-str argv, bool GUIenabled)

Constructs an application object with argc command line arguments in 
argv. If GUIenabled is true, a GUI application is constructed, otherwise 
a non-GUI (console) application is created.


More information about the PyQt mailing list