Important: Please read the Qt Code of Conduct -

Opengl with QGraphicsItem and QGraphicsScene?

  • Hi All,

    I have managed to draw two items who are QGraphicsitems on a QGraphicsView by a QGraphicsScene.
    I used QPainter to draw them, and everything is fine.
    Now I would like to draw these things by OpenGL, how hard is that ? and what should be actually changed from my code ?
    I should create a GLwidget window then Initialize GL,..etc, but how do I draw my previous items that were drew before on the opengl window using the same previous code?

  • I don't know how graphics with QGraphicsItems are created, but in general, you have to put in mind that drawing stuff in OpenGL is totally different, and I'm pretty sure that it has nothing to do with those Qt classes for a very simple reason, which is: when you draw with Qt you don't do 3D graphics and you don't do perspective preparations (3rd dimension projection). I think it's only 2D in Qt.

    Basically, OpenGL has nothing to do with Qt, but Qt has provided an OpenGL widget which is easily integrable into the other widgets of Qt, with some other methods in these widgets that help unify stuff between different operating systems.

    If you would like to draw "exactly" the same stuff with Qt and QGraphicsItem, that's OK. But please don't expect such a thing to be done "automatically", because I, myself, can't find a unique way to do that, which means, you have to make the thing yourself. You may create your own class that unifies plotting within 2 widgets in Qt, but each internal object of QGraphicsItem and QGLWidget has to be managed separately, perhaps through a single function or method, which would be your job as a developer.

    The good news is, OpenGL is not that hard, but is not that "simple" either. All I can tell you is that it's "something new", so you have to dedicate some time for that (a week maybe in the beginning?). In OpenGL, you have to draw simple objects like squares, circles, lines and rectangles from drafts, to achieve the amazing stuff we see in games and 3D videos. But you have to keep in mind that you're drawing in 3D space, which means you have to manage for the way stuff are projected to screen using glPerspective function manually (believe me there's not a unique way for doing that).

    My advice to you to learn OpenGL, is to start with Qt examples, parallel with this book (the redbook of OpenGL it's called):

    You don't have to buy the book, there are many older copies online. The newer versions of the book have changes in advanced stuff like shaders and anti-aliasing. So you just need the first few chapters, which are barely changed. So get any version online and start reading. When you finish the first 100 pages while checking Qt examples of OpenGL and doing the examples from the book on Qt, I promise you that you'll do everything you need to do as a beginner and you'll get the answer to your question :-)

    (and to be on the safe side, I'd say, if you like the book, buy it ^^)

    Good luck.

  • You can get the rendering of QGraphicsItems to use OpenGL by just setting the viewport with a QGLWidget.

    On the viewport, call setViewport(new QGLWidget).

    Hope that helps.

  • @TheDestroyer - 3d is just an illusion anyway, and there are plenty of ways to draw 3d in Qt, it is just that the 3d api is pretty basic, not very feature rich and really just intended for basic 3d, by no means a substitute for a game engine.

    AndreAhmed - there are plenty of examples on using OpenGL in Qt - just take a look. The "40000 chips": example in particular makes use of QGraphicsView and offers both software and OpenGL rendering

Log in to reply