Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3977332
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:54:40+00:00 2026-05-20T04:54:40+00:00

I am looking for a way to setup a number of timed function calls

  • 0

I am looking for a way to setup a number of timed function calls in pyqt, with a way to cancel all pending calls when needed.

I was looking at using QtCore.QTimer to setup the function calls, but I don’t know of any good way to cancel these calls. Any ideas?

I can’t seem to find any way to cancel after using QtCore.QTimer.singleShot(1000, self.function). If instead I create a list of QTimer objects I can stop them, but then I am forced to manage a list of timer objects (creation, deletion, which are active, etc.), which I would like to avoid.

# Setup a timer object.
timer = QtCore.QTimer(self)
timer.timeout.connect(self.function)
timer.setSingleShot(True)
timer.start(1000)

# To stop the timer object at some later point.
timer.stop()

I could also manage my own queue of pending function calls, which I would also like to avoid if possible (for the sake of simplicity).

Here is some dummy code showing the kind of thing I am trying to do:

import sys
from PyQt4 import QtCore

class Test(QtCore.QObject):

    def __init__(self, parent=None):
        QtCore.QObject.__init__(self, parent)
        

    def addDelayedCall(self, time, function):
        # Do something here.
        pass


    def clearPendingCalls(self):
        print('  Clearing pending function calls.')
        # Do something here.


    def setupCalls(self):
        self.addDelayedCall(500, self.dummy)
        self.addDelayedCall(1000, self.dummy)
        self.addDelayedCall(1500, self.dummy)
        self.addDelayedCall(2000, self.dummy)


    def dummy(self):
        print('dummy just got called.')


if __name__ == '__main__':
    app = QtCore.QCoreApplication(sys.argv)
    test = Test(app)

    QtCore.QTimer.singleShot(0, test.setupCalls)
    QtCore.QTimer.singleShot(1250, test.clearPendingCalls)
                   
    QtCore.QTimer.singleShot(5000, app.quit)            
    sys.exit(app.exec_())
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:54:40+00:00Added an answer on May 20, 2026 at 4:54 am

    I have come up with a solution by directly using the timer functionality provided by QObject. I suspect that there is a more elegant solution, but this will probably work for what I need.

    import sys
    from PyQt4 import QtCore
    
    
    class Test(QtCore.QObject):
    
        def __init__(self, parent=None):
            QtCore.QObject.__init__(self, parent)
    
            self.timers = {}
    
    
        def timerEvent(self, event):
            function = self.timers.pop(event.timerId())
            self.killTimer(event.timerId())
            function()
    
    
        def addDelayedCall(self, time, function):
            timer_id = self.startTimer(time)
    
            self.timers[timer_id] = function
    
    
        def clearPendingCalls(self):
            print('  Clearing pending function calls.')
    
            while self.timers:
                timer_id, function = self.timers.popitem()
                self.killTimer(timer_id)
    
    
        def setupCalls(self):
            self.addDelayedCall(500, self.dummy)
            self.addDelayedCall(1000, self.dummy)
            self.addDelayedCall(1500, self.dummy)
            self.addDelayedCall(2000, self.dummy)
    
    
        def dummy(self):
            print('dummy just got called.')
    
    
    
    
    if __name__ == '__main__':
        app = QtCore.QCoreApplication(sys.argv)
        test = Test(app)
    
        QtCore.QTimer.singleShot(0, test.setupCalls)
        QtCore.QTimer.singleShot(1250, test.clearPendingCalls)
    
        QtCore.QTimer.singleShot(5000, app.quit)            
        sys.exit(app.exec_())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm really looking for something very similar to the way SO is setup where
I am using codeigniter and looking a way to enable directly editting of doc
I'm currently using subversion and tourtiseSVN. I am looking to revamp the way my
I'm looking for a fast way to setup a method of returning a bitmask
I'm currently looking for a way to keep setup projects outputs from overwriting previous
Using the sample below, I am looking for a way how to count the
Looking for the best way to set-up an iPhone project in XCode ... namely:
I'm looking for way to create list view as like in IOS with pinned
I'm looking a way to enable IP logging with log4net in ASP.NET. I found
I'm looking for way to write Javascript programs / scripts on desktop, not inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.