Results 1 to 12 of 12

Thread: How to use multiple instances of a class containing QTimer?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2012
    Posts
    4
    Platforms
    Windows

    Default Re: How to use multiple instances of a class containing QTimer?

    Thanks for the useful hints! The problem did indeed come from the fact that I didn't have an events loop running... which means my qtimer::connect events didn't get processed. So I need to make a call to QCoreApplication::exec(), as shown here for example. Although it works now, it's made me run into another conceptual problem: is there any way of interrupting the qtimers prematurely (by pressing a key on the keyboard for example) since the application now requires the exec routine to terminate before moving on?

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: How to use multiple instances of a class containing QTimer?

    yes, there is a way to stop a timer
    http://doc.qt.io/qt-4.8/QTimer#stop
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Oct 2009
    Posts
    483
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 97 Times in 94 Posts

    Default Re: How to use multiple instances of a class containing QTimer?

    Now that your application has an event loop, you indeed need to call QCoreApplication::exit() or QCoreApplication::quit() for the program to exit. If I understand correctly, currently your application runs in a console, sets up some timers, some functions are called when they time out, and at some point QCoreApplication::quit() is called. Now you would also like your application to exit immediately when a certain key is pressed without waiting for the above scenario to complete. Is that correct?

    If it is the case, then you do not need to stop the timers. What you need is a way to handle some event (key press, or termination request from the OS) and make sure that QCoreApplication::quit() gets called. This is not trivial (and unfortunately platform-specific), but it may not be necessary. In its current state, your program is immediately killed if you hit Ctrl+C as it (currently) ignores the termination request. Of course this ends the program brutally but it is OK if no cleanup is required. It is definitely something to avoid if e.g. your program may be in the middle of writing to some files. So the main question is: is it acceptable if your program gets killed without having the chance to run some cleanup code?

  4. #4
    Join Date
    Jul 2012
    Posts
    4
    Platforms
    Windows

    Default Re: How to use multiple instances of a class containing QTimer?

    Yes, that's exactly what I need. However, instead of exiting the application entirely, I want it just to exit the events loop and then continue with the rest of the code in my main loop. In other words. is there a way of polling inside the Qtimer slot whether a key has been pressed and stopping the timer in consequence? Once the last timer has been stopped, quit the events loop and go on with the rest of the code?
    Perhaps my approach of using qtimers is not a good approach? Is it worthwhile looking into using QThreads instead?
    Thanks very much.

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: How to use multiple instances of a class containing QTimer?

    to deal with key press events you would normally have a gui and be using widgets. Otherwise you are either dealing with non-standard c++ headers or doing things like peek or cin.get.


    I don't know if QCoreApplication::winEventFilter could help you whilst not using any gui to handle key presses
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to use multiple instances of a class containing QTimer?

    Usually it's possible to monitor stdin using QSocketNotifier but it requires to disable buffering for stdin.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 2
    Last Post: 19th October 2011, 09:30
  2. Multiple instances of QWebView
    By darious in forum Qt Programming
    Replies: 1
    Last Post: 11th October 2011, 07:55
  3. Replies: 2
    Last Post: 2nd April 2011, 14:35
  4. Multiple program instances
    By scwizard in forum Qt Programming
    Replies: 13
    Last Post: 1st April 2007, 17:42
  5. accessing my own class-instances
    By mikro in forum Newbie
    Replies: 3
    Last Post: 11th July 2006, 00:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.