Results 1 to 1 of 1

Thread: How to achieve vsync using only QPainter on a QGLWidget?

  1. #1
    Join Date
    Jun 2015
    Qt products
    MacOS X Unix/X11 Windows Android

    Question How to achieve vsync using only QPainter on a QGLWidget?

    In my app, I need to update a text after a certain number of vertical refresh cycles (yes, I'm talking about v blank sync). I've never done anything OpenGL before, but I read about Qt's OpenGL system from several places and I've managed to write a class derived from QGLWidget as shown here. But unlike that stackoverflow answer, I've NOT USED paintGL,updateGL etc.. Instead, I've only overridden the paintEvent(QPaintEvent *) event and drawn the text purely using QPainter. But I'm not getting any "sync" effect. That is, my code is not triggering any update after ~16.6ms (I'm in a 60Hz setup). What happens, instead, is that my code update()s at random intervals like 5ms,1ms,23ms,17ms etc. And also the CPU usage is about 22%!! I know it (the high CPU usage) is because the timer updates at 0ms. But it was meant to mean "update ASAP", that is update as soon as vertical refresh occurs. But that thing is not happening. This is the code:

    Qt Code:
    1. QGLFormat generateFormat()
    2. {
    3. QGLFormat fmt;
    4. fmt.setAlpha(true);
    5. fmt.setSampleBuffers(true);
    6. fmt.setSwapInterval(1);
    7. return fmt;
    8. }
    10. MyClass::MyClass(QWidget *parent) : QGLWidget(generateFormat(), parent)
    11. {
    12. if (format().swapInterval() == -1)
    13. {
    14. timer.setTimerType(Qt::PreciseTimer);
    15. timer.setInterval(17);
    16. }
    17. else
    18. timer.setInterval(0);
    19. connect(&timer, SIGNAL(timeout()), this, SLOT(update()));
    20. timer.start();
    21. }
    23. void MyClass::paintEvent(QPaintEvent *e)
    24. {
    25. QPainter painter (this);
    26. painter.drawText(e->rect(), Qt::AlignCenter, "text to be drawn");
    27. }
    To copy to clipboard, switch view to plain text mode 

    What's wrong? I believe I've misunderstood something. But what is it?

    P.S.: Please don't say I've gotta do some spooky low-level openGL. I'm more than happy with QPainter.
    Last edited by wings; 28th June 2015 at 09:09.

Similar Threads

  1. Drawing with QPainter in a QGLWidget
    By jshafferman in forum Qt Programming
    Replies: 1
    Last Post: 2nd May 2014, 21:09
  2. Using QPainter in QGLWidget
    By Henzerr_visualplanet in forum Newbie
    Replies: 0
    Last Post: 1st April 2014, 15:50
  3. QPainter on QGLWidget
    By h123 in forum Qt Programming
    Replies: 2
    Last Post: 17th November 2008, 13:51
  4. about QGLWidget and QPainter
    By showhand in forum Qt Programming
    Replies: 5
    Last Post: 12th November 2008, 11:45
  5. [qt4,win,g++] QPainter on a QGLWidget
    By jh in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2008, 07:29

Tags for this Thread


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.