Results 1 to 4 of 4

Thread: [SOLVED]QTimer::remainingTime() returning negative numbers diff. from -1 ?!

  1. #1
    Join Date
    Aug 2013
    Posts
    9
    Qt products
    Qt5
    Platforms
    Windows

    Question [SOLVED]QTimer::remainingTime() returning negative numbers diff. from -1 ?!

    Hi everyone,
    I'm using a QTimer in a multithread Qt5 application. Here is my structure:

    Main application (MainWindow.cpp) creates a thread "ComputeScores" that creates N threads "AlgorithmTest" in its run() method. In each of these "AlgorithmTest" threads run() methods, I'm creating a timer to give to a particular algorithm some time budget (once the time is elapsed, the algorithm has to give the best answer it found during this interval).

    So what I do in some of my algorithm steps to check the budget isn't exhausted is testing that the reamining time of my timer is not 0. But it turned out that QTimer has a strange behaviour...
    When I start my algorithm in "AlgorithmTest" run(), the first thing I do is "myTimer.start(myBudget);". Then as second instruction, for testing purposes, I print myTimer.remainingTime() rightaway with "qDebug() << myTimer.remainingTime()".
    The first time it prints (so for the first call to start()), it prints... 0. Then the second time, it prints a random negative number (always a negative one) like -613230304. So of course the rest of my program just makes everything wrong (like infinite loop as the time never reaches 0, even if sometimes it does, despite of the negative remainingTime ?!).

    I just don't know what to do here, I googled that and nobody has never seen negative remaining times I think... :/

    Thank you in advance!!
    Last edited by Data42; 27th August 2013 at 23:10.

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

    Default Re: QTimer::remainingTime() returning negative numbers diff. from -1 ?!

    Show us the code please. Also remember that the timer requires a running event loop to fire. If you don't return to the event loop before the timer times out, you will not get notified that the timer has expired.
    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. #3
    Join Date
    Aug 2013
    Posts
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTimer::remainingTime() returning negative numbers diff. from -1 ?!

    Hi thank you for your response.
    You were right, the problem was the event loop that I didn't fired. Now instead of calling my code directly in run(), I just do
    Qt Code:
    1. QTimer::SingleShot(0, this, SLOT(threadCode());
    2. exec();
    To copy to clipboard, switch view to plain text mode 

    and I added "quit();" at the end of threadCode(). Everything is fine now.

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

    Default Re: QTimer::remainingTime() returning negative numbers diff. from -1 ?!

    How does that solve your stated problem?

Similar Threads

  1. Replies: 7
    Last Post: 15th February 2012, 13:51
  2. Negative numbers on QLineEdit
    By Cucus in forum Qt Tools
    Replies: 1
    Last Post: 22nd April 2011, 13:51
  3. How to use QLCDnumber to display negative numbers?
    By grissiom in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2010, 06:23
  4. Diff between QBrush and QPen
    By wagmare in forum Qt Programming
    Replies: 5
    Last Post: 16th February 2009, 14:47
  5. Diff between QImage, QPixmap, QPicture
    By nupul in forum Newbie
    Replies: 6
    Last Post: 30th April 2008, 02:02

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