Results 1 to 2 of 2

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 02:43.

Similar Threads

  1. QPainter on QGLWidget
    By h123 in forum Qt Programming
    Replies: 2
    Last Post: 17th November 2008, 12:51
  2. about QGLWidget and QPainter
    By showhand in forum Qt Programming
    Replies: 5
    Last Post: 12th November 2008, 10:45
  3. [qt4,win,g++] QPainter on a QGLWidget
    By jh in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2008, 06:29
  4. QGLWidget, QPainter and shared contexts
    By bamboo in forum Qt Programming
    Replies: 7
    Last Post: 17th April 2007, 18:11
  5. Simple input box for QPainter
    By therealjag in forum Qt Programming
    Replies: 1
    Last Post: 23rd February 2006, 15: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
  •  
Qt is a trademark of The Qt Company.