How to repeat a function call every X seconds in Pyqt?

Asked

Viewed 859 times

4

I am developing a small application in Pyqt4 where I need to upload the data to a table coming from a Webservice. This I managed to do quietly. However, now, I need that every 1 minute, these data be updated.

How can I do a repeat of a function call every X seconds in Pyqt4?

Is there anything specific to this in Pyqt? (something similar to Javascript setInterval)

Note: The data reload process in this interval has to be asynchronous

  • 2

    I don’t have Pyqt installed, so I won’t even risk answering. But the way is to use QTimer. :) http://stackoverflow.com/questions/32362340/pyqt4-creating-a-timer

1 answer

5


One can use the QTimer of Qt. In the Pyqt4 documentation I did not find examples, but I believe it should look like this (every 1 second):

from PyQt4.QtCore import QTimer

def chamar():
    print 'Foo'

timer = QTimer()
timer.timeout.connect(chamar)
timer.start(1000)

Yet I believe it’s best to use singleShot, because if a function takes longer than the timeout there will be no chance of it being executed two or more simultaneously (which can cause conflicts and "crashes"), I believe it stays that way:

from PyQt4.QtCore import QTimer

def chamar():
    # Aqui fica o código que será executado

    timer = QTimer()
    timer.setSingleShot(True)
    timer.timeout.connect(chamar)
    timer.start(1000)

chamar()
  • 1

    Really, the last option is the best choice. When it comes to http requests, which can hang a little more than it should, it is always good to call the function again only if the answer is OK.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.