Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Opengl with QGraphicsItem and QGraphicsScene?
Forum Update on Monday, May 27th 2025

Opengl with QGraphicsItem and QGraphicsScene?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 9.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AndreAhmed
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      TheDestroyer
      wrote on last edited by
      #2

      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):

      http://www.amazon.de/OpenGL-Programming-Guide-Official-Learning/dp/0321552628

      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.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mr-Merlin
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • ? This user is from outside of this forum
          ? This user is from outside of this forum
          Guest
          wrote on last edited by
          #4

          @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":http://qt-project.org/doc/qt-4.8/demos-chip.html example in particular makes use of QGraphicsView and offers both software and OpenGL rendering

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved