Results 1 to 5 of 5

Thread: QTimer Question

  1. #1
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Red face QTimer Question

    I have an application in Qt-4 with OpenGL. And I want to have in the title bar the FPS. But I can't find any examples on source of how to implement it.
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer Question

    Then stop looking for examples and think about the solution. How to calculate FPS?

  3. #3
    Join Date
    Jul 2008
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer Question

    Qt Code:
    1. QTime current_time;
    2. QTime last_time;
    3. int elapsed_time;
    4. float fps; // current fps in the loop, depending on the "work time" required during the paintGL call
    5.  
    6. void gldrawer::paintGL()
    7. {
    8. last_time = current_time;
    9. current_time = QTime::currentTime();
    10. elapsed_time = (current_time.second()*1000 + current_time.msec()) - (last_time.second()*1000 + last_time.msec()); // /!\ max = 59 seconds 999 ms
    11. this->fps = 1000 / elapsed_time;
    12.  
    13.  
    14.  
    15. /*
    16. Si on souhaite limiter l'affichage à X fps pour éviter des cycles processeurs inutiles, il faut :
    17. [code non testé, mais l'idée est là ]
    18.  
    19. static FPS = 30; // je veux limiter à 30FPS
    20. static int time_per_frame = 1000/FPS;
    21.  
    22. if(elapsed_time < time_per_frame)
    23. QTest::qWait(time_per_frame - elapsed_time);
    24. */
    25.  
    26.  
    27.  
    28. .... painting stuff ....
    29. }
    To copy to clipboard, switch view to plain text mode 

    n'joy
    this work only if you redraw every time the scene.
    For example in the constructor :

    Qt Code:
    1. QTimer qTimerRedraw; // not QTime but QTimer
    2. connect(&qTimerRedraw, SIGNAL(timeout()), this, SLOT(paintGL()));
    3. qTimerRedraw.start(0);
    To copy to clipboard, switch view to plain text mode 



    But i'm currently having problems with this (resize events, moving the window, ...).
    See : http://www.qtcentre.org/forum/f-qt-p...html#post75079
    Last edited by jpn; 3rd July 2008 at 13:56. Reason: missing [code] tags

  4. The following user says thank you to anthibug for this useful post:

    ComaWhite (1st August 2008)

  5. #4
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QTimer Question

    Thank you so much. I didn't think anyone would ever respond to it. I'll give it a try later on today and let you know. =)
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  6. #5
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QTimer Question

    I noticed that it works great. But only two problems. The FPS clock when putting in the titlebar is goes too quickly to even see it and when I disabled vsync in the nVidia control panel it only went to 1000 and thats it.

    Qt Code:
    1. #ifndef COMATIMER_HPP
    2. #define COMATIMER_HPP
    3.  
    4. #include "comaglobal.hpp"
    5. #include <QTime>
    6.  
    7. COMA_BEGIN_NAMESPACE
    8.  
    9. class COMA_DLL Timer
    10. {
    11. public:
    12. Timer()
    13. {
    14. reset();
    15. }
    16.  
    17. ~Timer()
    18. {
    19. }
    20.  
    21. void reset()
    22. {
    23. m_zeroClock = QTime::currentTime();
    24. m_startClock = QTime(0, 0, 0);
    25. }
    26.  
    27. ULong milliseconds()
    28. {
    29. QTime now = QTime::currentTime();
    30. return (now.second() - m_startClock.second()) * 1000 + (now.msec() - m_startClock.msec()) / 1000;
    31. }
    32. private:
    33. QTime m_zeroClock;
    34. QTime m_startClock;
    35.  
    36. };
    37.  
    38. COMA_END_NAMESPACE
    39.  
    40. #endif /*COMATIMER_HPP*/
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void
    2. ComaGL::paintGL()
    3. {
    4. ++g_fps;
    5. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    6. glLoadIdentity();
    7.  
    8. Vector3 eye(0.0, 0.0, -15.0);
    9. Vector3 centre(0.0, 0.0, 0.0);
    10. Vector3 up(0.0, 1.0, 0.0);
    11.  
    12. Matrix4 modelViewMatrix(Matrix4::M4_Identity);
    13. modelViewMatrix.lookAt(eye, centre, up);
    14.  
    15. glMultMatrixf(modelViewMatrix.data());
    16.  
    17. Matrix4 rot(Matrix4::M4_Identity);
    18. rot.rotateX(g_angle);
    19. glMultMatrixf(rot.data());
    20. glEnableClientState(GL_VERTEX_ARRAY);
    21. glEnableClientState(GL_COLOR_ARRAY);
    22.  
    23. glVertexPointer(3, GL_FLOAT, 0, &m_tri[0]);
    24. glColorPointer(3, GL_FLOAT, 0, &m_colour[0]);
    25. glDrawArrays(GL_QUADS, 0, 4);
    26.  
    27. glDisableClientState(GL_COLOR_ARRAY);
    28. glDisableClientState(GL_VERTEX_ARRAY);
    29.  
    30. g_angle += 2.0;
    31.  
    32. g_currentTime = g_timer.milliseconds();
    33. ULong time = g_currentTime - g_lastTime;
    34.  
    35. if(g_currentTime - g_lastTime > 1000)
    36. {
    37. setWindowTitle(QString("Fps: %1")
    38. .arg((UInt32)(g_fps / (Real)(g_currentTime - g_lastTime) * 1000.0)));
    39. g_lastTime = g_currentTime;
    40. g_fps = 0;
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ComaWhite; 1st August 2008 at 20:34.
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 12:51
  2. Extending QTimer()
    By rishid in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2009, 02:59
  3. QThread/QObject and QTimer
    By swiety in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2008, 09:37
  4. Replies: 5
    Last Post: 6th March 2007, 06:34
  5. Question about using the QextSerialPort~
    By coffeemorphism in forum Qt Programming
    Replies: 2
    Last Post: 1st February 2007, 15:16

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.