Results 1 to 7 of 7

Thread: Is QTimer is the only option ?

  1. #1
    Join Date
    Sep 2008
    Posts
    58
    Thanks
    11
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Post Is QTimer is the only option ?

    Hi,

    I want to poll continuously some hardware functions like ..

    Qt Code:
    1. main(int argc, char *argv[])
    2. {
    3. Widget w; // Widget Class
    4. QApplication a(argc, argv);
    5. while (1)
    6. {
    7. //functions to poll continuously
    8. }
    9.  
    10. w.show();
    11. a.exec();
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    If i execute above code it doesn't show widget on the display, I understand that a.exec() should execute to enter in to the event loop.

    But if i write like this...

    Qt Code:
    1. main(int argc, char *argv[])
    2. {
    3. Widget w; // Widget Class
    4. QApplication a(argc, argv);
    5.  
    6. w.show();
    7. while (1)
    8. {
    9. //functions to poll continuously
    10.  
    11.  
    12. a.exec();
    13. }
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    in while loop only execute once, and then a.exec() wait for the event.
    How can I run while loop continuously to poll some function.
    Is it mean that I need to start QTimer and need timer event every time?

    Or is there any other way that can use while loop?

    With Regards,
    Nirav

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is QTimer is the only option ?

    no u need a seperate event loop along with the main gui event loop ... try QThread() ... it creates a new event loop and communicate with sig/slot ..

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

    Default Re: Is QTimer is the only option ?

    Maybe you can use QSocketNotifier?
    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.


  4. #4
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Is QTimer is the only option ?

    Perhaps you could put your while loop inside the widget class and not on the main function

  5. #5
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Is QTimer is the only option ?

    Quote Originally Posted by nrabara View Post
    [...]

    If i execute above code it doesn't show widget on the display, I understand that a.exec() should execute to enter in to the event loop.
    That is because you never reach the call to a.exec(). This code continuously executes the loop, and as it can never leave the loop because the loop's condition is always true, the code beyond is never executed.

    Quote Originally Posted by nrabara View Post
    [...]

    in while loop only execute once, and then a.exec() wait for the event.
    How can I run while loop continuously to poll some function.
    Is it mean that I need to start QTimer and need timer event every time?
    Here a.exec() starts a new loop, that is why your original loop is only executed once.

    If you want to display some data polled from a device you should probably use a seperate thread that does the polling and notifies the GUI via a signal if something changed. Have a look at the thread examples to see how this is done.

  6. #6
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is QTimer is the only option ?

    Here's a link to quite interesting wysota's article in QQ, which may be useful here:
    http://doc.trolltech.com/qq/qq27-responsive-guis.html
    Last edited by wysota; 5th May 2009 at 20:24. Reason: reformatted to look better
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  7. The following user says thank you to calhal for this useful post:

    wagmare (6th May 2009)

  8. #7
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is QTimer is the only option ?

    right mr.calhal ... its a very good article to learn the event loop in qt ...

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Extending QTimer()
    By rishid in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2009, 01:59
  3. QThread/QObject and QTimer
    By swiety in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2008, 08:37
  4. Replies: 5
    Last Post: 6th March 2007, 05:34
  5. QGLWidget with multiple monitors
    By Rayven in forum Qt Programming
    Replies: 3
    Last Post: 4th August 2006, 10:28

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.