Results 1 to 2 of 2

Thread: QPainter very slow on QGLWidget even in simple example?

  1. #1
    Join Date
    Jan 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question QPainter very slow on QGLWidget even in simple example?

    As far as I understood it, QPainter would be a convenient way to draw simple shapes in my GL widget without going through the hassle of individual vertices, but also without any significant loss of performance. However, in a simple rectangle test, native OpenGL still seems to be faster by a factor of 30. Am I doing something wrong?

    Here's the code of my QGLWidget's paintEvent. The vector testpos contains 10'000 points as of now. I would expect no changes in the OpenGL state between the drawRect calls.

    Qt Code:
    1. void GLWidget::paintEvent(QPaintEvent *event)
    2. {
    3. makeCurrent();
    4. qglClearColor(Qt::black);
    5. setupViewport(width(), height());
    6. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    7.  
    8. // draw rectangles using native OpenGL code
    9. // normalized coordinates
    10. QTime stopwatch;
    11. stopwatch.start();
    12. glColor4f(0.0f,0.0f,1.0f,1.0f);
    13. foreach(const QPointF * pt, testpos)
    14. {
    15. glBegin(GL_LINE_LOOP);
    16. glVertex2f(pt->x(), pt->y());
    17. glVertex2f(pt->x()+0.03, pt->y());
    18. glVertex2f(pt->x()+0.03, pt->y()+0.03);
    19. glVertex2f(pt->x(), pt->y()+0.03);
    20. glEnd();
    21. }
    22. qDebug() << "elapsed msec native gl: " << stopwatch.elapsed();
    23.  
    24.  
    25. // draw rectangles using QPainter
    26. // screen space coordinates
    27. stopwatch.start();
    28. QPainter p(this);
    29. p.setPen(Qt::blue);
    30. foreach(const QPointF * pt, testpos)
    31. {
    32. p.drawRect(pt->x()*500, pt->y()*500, 20, 20);
    33. }
    34. p.end();
    35. qDebug() << "elapsed msec painter: " << stopwatch.elapsed();
    36. }
    To copy to clipboard, switch view to plain text mode 

    The output I get is:
    elapsed msec native gl: 2
    elapsed msec painter: 65
    elapsed msec native gl: 2
    elapsed msec painter: 76
    ...

    Omitting one or the other drawing method doesn't change anything, and it's not a measurement error. The delay scales with the number of rectangles, so it has to be a problem in the drawRect. What could be wrong here?

    Best wishes,
    - Dominik
    Last edited by dpkay; 24th January 2010 at 03:43.

  2. #2
    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: QPainter very slow on QGLWidget even in simple example?

    I don't think anything is wrong. Qt has to translate QPainter calls to OpenGL routines which takes time and final calls obtained might be different. And it's hard to call it a test since you use different objects and figures for each of the loops. Mere multiplication of a floating point number and an integer takes time. If you do it many times, it starts to become significant. Furthermore a single loop is not enough for a benchmark because of QTime precision. Repeat each loop 1000 times and measure the time of that and then divide the result by 1000 and maybe you'll obtain something closer to the truth.
    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. QPainter on QGLWidget
    By h123 in forum Qt Programming
    Replies: 2
    Last Post: 17th November 2008, 13:51
  2. about QGLWidget and QPainter
    By showhand in forum Qt Programming
    Replies: 5
    Last Post: 12th November 2008, 11:45
  3. [qt4,win,g++] QPainter on a QGLWidget
    By jh in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2008, 07:29
  4. QGLWidget, QPainter and shared contexts
    By bamboo in forum Qt Programming
    Replies: 7
    Last Post: 17th April 2007, 19:11
  5. Simple input box for QPainter
    By therealjag in forum Qt Programming
    Replies: 1
    Last Post: 23rd February 2006, 16:33

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.