How can I kill one shot of QtCore.QTimer in PyQt4?

So, in my application, I create a single object QtCore.QTimerand then call the method singleShotto call the function after 60 seconds. Now, at any point in time, if I need to call the method again singleShotand prevent the previous method from being used singleShot(which prevents it from calling the caller passed to it if the second time singleShotis called before the first 60 seconds), what should I do? How can I “kill” the previous one QTimerand completely forget about it and work only with the current one QTimer?

Can someone help me with this?

Here is 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'

thanks

+4
4

, QTimer.singleShot() QTimer. , , . QTimer ( , , singleShot QTimer QTimer, .)

. QTimer setSingleShot(True). stop(), . . , , , - 3 . , , , "" , 3 . , 4 , , , !

, !

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_()
+10

QTimer , . , , , .

QBasicTimer. :

QTimer . QBasicTimer, , QTimer, , .

current_timer, current_timer.deleteLater. , QTimer, , del/ del current_timer, - , , - C++ object not found.

+1

@three_pineapples answer .
QTimer , .start() , .

. PyQt4 QTimer.start().

import sys

from PyQt4.QtCore import QTimer
from PyQt4.QtGui import (
    QApplication,
    QWidget,
    QVBoxLayout,
    QPushButton,
)

class MyApp(QWidget):
    def __init__(self,*args,**kwargs):
        QWidget.__init__(self,*args,**kwargs)

        self.layout = QVBoxLayout(self)

        self.button = QPushButton('Start timer')
        self.button.clicked.connect(self.start_timer)

        self.layout.addWidget(self.button)

        self.timer = QTimer()
        self.timer.timeout.connect(self.hello)
        self.timer.setSingleShot(True)

    def start_timer(self):
        self.timer.start(3000)

    def hello(self):
        print('Hello!')

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

from PyQt4.QtCore import QTimer
from PyQt4.QtGui import (
QApplication,
QWidget,
QVBoxLayout,
QPushButton,)

class MyApp(QWidget):

    def __init__(self,*args,**kwargs):
        QWidget.__init__(self,*args,**kwargs)

        self.layout = QVBoxLayout(self)

        self.button = QPushButton('Start timer')
        self.button.clicked.connect(self.start_timer)

        self.button1 = QPushButton('Stop timer')
        self.button1.clicked.connect(self.stop_timer)

        self.layout.addWidget(self.button)
        self.layout.addWidget(self.button1)
        self.timer = QTimer()
        self.timer.timeout.connect(self.hello)
        self.timer.setSingleShot(False)


    def start_timer(self):
        self.timer.start(1000)

    def stop_timer(self):       
        self.timer.stop ()
        print('timer stop')

    def hello(self):
        b=1   
        print(a)
        a.append(b)
        print(a)
        if len(a) == 5:
            self.timer.stop ()
            print('timer stop')


a=[]
# Create QApplication and QWidget
qapp = QApplication(sys.argv)  
app = MyApp()
app.show()
qapp.exec_()
-1

All Articles