如何在 PyQt4 中杀死单发 QtCore.QTimer? [英] How can I kill a single shot QtCore.QTimer in PyQt4?

查看:25
本文介绍了如何在 PyQt4 中杀死单发 QtCore.QTimer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我的应用程序中,我创建了一个 QtCore.QTimer 对象,然后在它上面调用 singleShot 方法以在 60 秒后调用一个函数.现在,在任何给定的时间点,如果我需要再次调用 singleShot 方法并阻止之前的 singleShot 方法生效(即阻止它调用传递给它的调用者,如果第二次 singleShot 在第一个 60 秒之前被调用),我需要做什么?我怎样才能杀死"以前的 QTimer 并完全忘记它并只使用当前的 QTimer?

So, in my application, I create a single QtCore.QTimer object and then call the singleShot method on it to evoke a function after say 60 secs. Now, at any given point in time, if I would need to call the singleShot method on it again and prevent the previous singleShot method from taking effect (that is preventing it from calling the caller passed to it, if the second time the singleShot is invoked before the first 60 seconds), what do I need to do? How can I 'kill' the previous QTimer and completely forget about it and work only with the current QTimer?

有人可以帮我解决这个问题吗?

Can someone help me out with this?

这里只是一个示例代码:

Here's just a sample code:

def main():
    q = QtCore.QTimer()
    q.singleShot(4000, print_hello)
    q.killTimer(id)     ##how can I get the value of 'id' so that print_hello() is not called before the end of the 4 seconds?

def print_hello():
    print 'hello'

谢谢

推荐答案

问题在于 QTimer.singleShot() 没有返回对 QTimer 的引用.我不知道无论如何要获取计时器 ID,因此您可以使用该方法将其杀死.但是,您可以实例化一个普通的 QTimer 并使其成为单次定时器(这不是您在提供的代码中所做的,在 singleShot 的实例上调用 singleShotcode>QTimer 创建一个您无权访问的 new QTimer.)

The problem is that QTimer.singleShot() does not return a reference to the QTimer. I don't know of anyway to get the Timer ID so you can kill it using that method. However, you can instantiate a normal QTimer and make it a single-shot timer (this is not what you have done in your code provided, calling singleShot on an instance of QTimer creates a new QTimer which you do not have access to.)

然而,一切都没有丢失.您可以创建一个普通的 QTimer 并使用 setSingleShot(True) 将其转换为单发定时器.如果您希望中止计时器,这允许您调用 stop() 方法.请参阅下面的代码示例,该示例在 3 秒超时时执行您的要求.您可以快速连续多次按下按钮,停止后会打印一次你好",3秒后.推一次,等4秒,再推,当然会打印两次!

However, all is not lost. You can create a normal QTimer and convert it to a single shot timer using setSingleShot(True). This allows you to call the stop() method if you wish to abort the timer. See the code example below which does what you require, on a 3 second timeout. You can push the button as many times as you like in rapid succession, and it will print "hello" once, 3 seconds after you stop. If you push it once, wait 4 seconds, and push it again, it will of course print out twice!

希望有帮助!

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class MyApp(QWidget):
    def __init__(self,*args,**kwargs):
        QWidget.__init__(self,*args,**kwargs)
        self.current_timer = None
        self.layout = QVBoxLayout(self)
        self.button = QPushButton('start timer')
        self.button.clicked.connect(self.start_timer)
        self.layout.addWidget(self.button)

    def start_timer(self):
        if self.current_timer:
            self.current_timer.stop()
            self.current_timer.deleteLater()
        self.current_timer = QTimer()
        self.current_timer.timeout.connect(self.print_hello)
        self.current_timer.setSingleShot(True)
        self.current_timer.start(3000)

    def print_hello(self):
        print 'hello'


# Create QApplication and QWidget
qapp = QApplication(sys.argv)  
app = MyApp()
app.show()
qapp.exec_()

这篇关于如何在 PyQt4 中杀死单发 QtCore.QTimer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆