Search code examples
pyqt5pyopengl

Draw on an image: PyQt and PyOpenGL


I'm trying to Draw line and shapes on an existing image using PyQt5 and PyOpenGL. I have done the following so far:

  1. create an object of QGraphicsView
  2. add an QGraphicsScene() object to it
  3. add an Pixmap into my scene

I also created a class called GLWidget which inherits from QGLWidget. The problem is that I can not display the GLWidget in the scene. I mean when I do scene.addWidget(glWidget) the functions (initializeGL,paintGL,...) are called but nothing shows on the screen. I have to say that when I only display the GLWidget like this:

glWidget = GLWidget(self)
self.setCentralWidget(glWidget)

it works fine!

Thanks in advance.


Solution

  • I found the solution in PyQt5 examples in github here: PyQt5 OpenGL examples

    The tricky part was in the initializeGL function which I had to do:

    self.gl = self.context().versionFunctions()
    self.gl.initializeOpenGLFunctions()
    ....
    

    I hope it helps!