質問

How can I properly use the QtTimer?

I have a button connect to a method which basicly does some math, but I want to iterate over it and see the result one at a time, with a delay of 1 second between them.

In normal scriping i would use

for i in xrange(10):
   domath(i)
   sleep(1)

But in GUI it doesn't work because it freezes the GUI and I can't see the result. Only after 10 seconds I see the last iteration.

I tried the QtTimer but it doesn't seems to work.

for i in xrange(10):
    QtCore.QTimer.singleShot(1000,lambd: domath(i))

it goes directly to the last iteration. Am I missing something?

役に立ちましたか?

解決

I would personally write something like this:

class A(QObject):
    def __init__(self):
        self.counter = 0
        Timer.singleShot(100, self.domath())

    Slot()
    def domath(self):
        # do the computation
        self.counter += 1
        if self.counter != 10:
            Timer.singleShot(100, self.domath())
        else:
            self.counter = 0;

Disclaimer: even though it looks like PyQt code, this is only pseudo code. I have never tested, etc, but the concept is valid in my opinion.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top