Results 1 to 3 of 3

Thread: right way to use a QTimer on a console application.

  1. #1
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default right way to use a QTimer on a console application.

    I have a console application that looks for a device on the network and gets data from it. It will either get the data within a few seconds or it get stuck in trying to receive data.

    So I wanted to set up a Qtimer that would preform a quit after 10 seconds. Here is my code but it does not seem to work.

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. QTimer::singleShot(10000, &a, SLOT(quit()));
    6.  
    7. // either will return within 3 seconds or get stuck trying to do a read.
    8. LLDP::Execute();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    It is pretty much exactly what is in the singleShot documentation. My code is where the "..." are.

    Qt Code:
    1. #include <QApplication>
    2. #include <QTimer>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. QTimer::singleShot(600000, &app, SLOT(quit()));
    8. ...
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong and what is the correct way?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: right way to use a QTimer on a console application.

    Please note that if your Execute() call "gets stuck", a.exec() never has a chance to execute hence your timer will not work. Timer alone can't solve your problem. You'd have to run your Execute() code in external thread while the main thread processes events allowing the timer to fire.
    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.


  3. The following user says thank you to wysota for this useful post:

    weaver4 (15th February 2012)

  4. #3
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Re: right way to use a QTimer on a console application.

    Thanks, that did it.

    Did not realize that the Timer would not work unless a.exec() was executed.

Similar Threads

  1. about console application
    By jirach_gag in forum Qt Programming
    Replies: 2
    Last Post: 5th January 2012, 11:39
  2. qtimer in console application
    By JeanC in forum Newbie
    Replies: 10
    Last Post: 31st March 2011, 15:30
  3. Replies: 2
    Last Post: 21st November 2010, 18:03
  4. QTimer - too many in application??
    By db in forum Newbie
    Replies: 4
    Last Post: 31st March 2008, 15:12
  5. run a console application through a gui interface
    By gurinder in forum Qt Programming
    Replies: 3
    Last Post: 5th February 2008, 12:52

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.