Important: Please read the Qt Code of Conduct -

What the fastest way to draw 2d graphic in qt?

  • What the fastest way to draw 2d graphic in qt? Where I can reading about it?
    Sorry for my bad English.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    It will depend on what you want to draw. Usually OpenGL is the fastest.

  • @SGaist
    Thanks. I want draw 2000 lines of different colors more than 25 time/second. What way the best for my target?

  • Hi,

    Please correct me if I'm wrong (thus, I will learn too).
    If you only want to draw lines, I'm not sure that using OpenGL is necessary, and its implementation and use can be quite tricky.

    Personnaly, I would go for a pure Qt approach :

    • Use a QTimer to call your drawing procedure at the frequency you need.
    • Draw your lines on a QImage with a loop. (QImage are optimised for direct access to pixels and are therefore made for drawing).
    • Make a QPixmap from this image (QPixmap are optimised for display).
    • Display you QPixmap in a widget. As far as I'm concerned, I use a QGraphicsScene to receive my QPixmap, and then I display it in a QGraphicsView (once again, I would be glad to learn a better way).

    For now, with the relative knowledge I have, nothing makes me think that OpenGL would be faster for 2D, because you would have pretty much the same procedure :

    • create your image on a matrix (apply a projection matrix to render it ? projection matrices are used in 3D to "flatten" the scene, but can we ignore them in 2D ? I don't know)
    • save the image on a backbuffer
    • swap the frontbuffer and the backbuffer
    • display it in an OpenGLWidget.

    The procedure is not shortened using OpenGL, so is there such a big optimization difference between Qt and OpenGL in the case of 2D drawing ?

    OpenGL is more complex than Qt, so I suggest that unless you absolutely need it, use Qt instead.
    Testing is the best way to know ! Try using Qt, and if the result is too slow, try using OpenGL (or should I say 'try to use OpenGL' ;-) ).

  • @Proteos Thanks very mach! It's very intresting! I will try it.

Log in to reply