Results 1 to 14 of 14

Thread: QTimer don't start

  1. #1
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation QTimer don't start

    Hi

    I have a problem with QTimer, I call the start function but don't work:

    Qt Code:
    1. class MyClass : public QObject {
    2.  
    3. Q_OBJECT
    4.  
    5. private:
    6. QTimer* timer;
    7.  
    8. private slots:
    9. void executeMyTimer();
    10.  
    11. ....
    12. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. MyClass::MyClass() : QObject() {
    2. this->timer = new QTimer(this);
    3. this->timer->setInterval(500);
    4. connect(this->timer, SIGNAL(timeout()), this, SLOT(executeMyTimer()));
    5. this->timer->start();
    6. }
    To copy to clipboard, switch view to plain text mode 

    I've tried to use QBasicTimer, QTimer::singleShot() and QObject::startTimer() but don't work...

    I think that the problem is that my app have an infinite loop. I call the run() function like this:

    Qt Code:
    1. int main(int argc, char** argv) {
    2. QApplication app(argc, argv);
    3.  
    4. MyApp app;
    5. QTimer::singleShot(0, &app, SLOT(run()));
    6.  
    7. return app.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    And the run function:
    Qt Code:
    1. MyApp::run() {
    2. // .....
    3.  
    4. for(;;) {
    5. // ...
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    Somebody can help me please?

  2. #2
    Join Date
    Apr 2009
    Posts
    5
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTimer don't start

    An infinite loop would definitely be a problem. QTimer will fire only when the execution of the program is in an event loop, it can't interrupt infinite loops. Why do you have the infinite loop anyway?

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

    Qn00b (5th February 2011)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTimer don't start

    I assume you instantiate your MyClass somewhere inside run(). You could use QCoreApplication::processEvents() inside your loop to give the timer signals a chance to be processed. I suspect rethinking the design would a better place to put some effort.

  5. The following user says thank you to ChrisW67 for this useful post:

    Qn00b (5th February 2011)

  6. #4
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer don't start

    Hi! Thank you very much for your help!

    I assume you instantiate your MyClass somewhere inside run()
    Yes.

    Why do you have the infinite loop anyway?
    I need to read hardware events... For example, is similar to read keys, you need to listen the keyboard all time and process the key pressed.

    You could use QCoreApplication:rocessEvents() inside your loop to give the timer signals a chance to be processed.
    No, this don't work because the function to read keys (as in the example) blocks the Qt event loop.


    I will try to launch the infinite loop in other thread, although I think that isn't the best solution..

    Any other idea?
    Thanks!

  7. #5
    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: QTimer don't start

    Can't you just forget about the infinite loop and use keypressevent() to read the keyboard, or am I missing something ?
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  8. The following user says thank you to john_god for this useful post:

    Qn00b (5th February 2011)

  9. #6
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer don't start

    No I can't. Really the application don't read keys, is an example of the problem.

    I have a non Qt function to catch hardware events that blocks the Qt event loop and disallow me to use timers...

    Thanks!

  10. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer don't start

    If your writing this on Windows, then you can have workable timers without an event loop - Windows will call your application code directly, bypassing any infinite loop you have in your code. However, if your timer function is too long, it can have disastrous consequences on the OS.

  11. The following user says thank you to squidge for this useful post:

    Qn00b (5th February 2011)

  12. #8
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer don't start

    No, I need to develop it for GNU/Linux... And confirmed, launch the infinite loop in a thread crash the application (I use a C library and don't support threads...)

    Anyway thank you very much!!

  13. #9
    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: QTimer don't start

    Read this article, maybe it will give you some insight: [wiki]Keeping the GUI Responsive[/wiki].
    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.


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

    Qn00b (7th February 2011)

  15. #10
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer don't start

    I try all methods, none worked...

    I will try to explain in detail the problem...
    I have a class with a run() method and a static callback method. The callback method is called when detects a change in hardware:

    Qt Code:
    1. int main(int argc, char** argv) {
    2. QApplication app(argc, argv);
    3.  
    4. HWHandler hwh;
    5. QTimer::singleShot(0, &hwh, SLOT(run()));
    6. // I have also tested -> hwh.start();
    7.  
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class HWHandler { // I have also tested -> : public QThread
    2. public:
    3. void run();
    4. static void ProcessHwEvent();
    5. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void HWHandler::run() {
    2. forever {
    3. if(waitHWEvent()) // This function returns ONLY when a HW event occurs, if no hw event, no return and freeze the Qt event loop
    4. getHWEvent(); // Calls to ProcessHwEvent() that process the HW event
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

    And in ProcessHwEvent() I call to another class that uses QTimers... QTimer that don't work...


    I try to put the run() code in a thread, but the Qt event loop continuous frezee...
    Any idea for what the Qt event loop is freeze?
    Any solution?

    Thanks!

  16. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTimer don't start

    Why not have your blocking thread just queue a signal to the main GUI thread when something changes and continue the processing there? That is, getHwEvent just collects some information and emits a suitable queued signal that is connected to a slot in the main thread.

    What is this blocking hardware change function in a Linux environment?
    Why is ProcessHwEvent() static?

  17. The following user says thank you to ChrisW67 for this useful post:

    Qn00b (7th February 2011)

  18. #12
    Join Date
    Apr 2009
    Posts
    5
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTimer don't start

    Quote Originally Posted by Qn00b View Post
    And in ProcessHwEvent() I call to another class that uses QTimers... QTimer that don't work...
    I don't think you can/should use QTimers in ProcessHwEvent() because it's still a part of the infinite loop. Even if you put the loop in another thread (which you should do) any QTimer object you create is going to live in the wrong thread unless you move it to the main thread with QObject::moveToThread().

    I try to put the run() code in a thread, but the Qt event loop continuous frezee...
    Any idea for what the Qt event loop is freeze?
    Any solution?
    Can you show the code? Because you're not giving us much information I'm guessing that you made HWHandler::run() public and called it directly from the main thread.

  19. The following user says thank you to nirva for this useful post:

    Qn00b (7th February 2011)

  20. #13
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer don't start (solved)

    Hi! Finally I have been able to solve the problem! I put the infinite loop in a thread and I have made that ProcessHwEvent() sends signals to other class. With signals all is ok, the problem was that I called directly to other functions.

    Thanks very much to all!!

  21. #14
    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: QTimer don't start (solved)

    You might have read the article and you might have "tried everything" but you didn't follow any of the advices from the article.

    The easiest solution to your "problem" is to avoid calling waitHWEvent() which I bet is possible using solution such as QSocketNotifier or something similar. Using threads is a "sort-of" solution to the "problem" but in reality it doesn't solve the problem just works around it in some of the cases. My immediate question is how do you exit the thread if you want to finish the program? With your current "solution" the only thing you can do is to kill it the hard way.
    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. QTimer in QThread doesn't start or timeout
    By Boron in forum Qt Programming
    Replies: 9
    Last Post: 21st October 2011, 13:51
  2. Replies: 8
    Last Post: 10th December 2009, 10:06
  3. Use of QTimer
    By A.H.M. Mahfuzur Rahman in forum Qt Programming
    Replies: 4
    Last Post: 7th August 2009, 13:52
  4. QTimer
    By dragon in forum Qt Programming
    Replies: 18
    Last Post: 16th November 2008, 14:15
  5. QTimer ->start(0) + OpenGL + resize/move window => crash
    By anthibug in forum Qt Programming
    Replies: 5
    Last Post: 8th July 2008, 11:01

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.