Painting over a QGLWidget fails
-
I am trying to paint over a QGLWidget by means of a QPainter as explained in various tutorials. In my paintGL() function, I have two cases. Indeed, if there is nothing to draw in OpenGL then I only use the QPainter to draw 2 lines (this part does work). However, when there is something to draw with OpenGL, I first use an OpenGL function such as drawElements() and then I use my painter to overpaint the widget, but in this case I can only display my OpenGL "objects", the two lines being invisible.
Here is the code of my paintGL method:
def paintGL(self): glClearColor(0, 0, 0, 1) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) if np.array(self.objects).size: print('there are %i objects'%(len(self.objects))) # # active shader program glUseProgram(self.shaderProgram) for i, obj in enumerate(self.objects): loc_pos = glGetAttribLocation(self.shaderProgram, "position") glEnableVertexAttribArray(loc_pos) glBindBuffer(GL_ARRAY_BUFFER, obj.VBO[0]) glVertexAttribPointer(loc_pos, 2, GL_FLOAT, False, 0, ctypes.c_void_p(0)) loc_col = glGetAttribLocation(self.shaderProgram, "color") glEnableVertexAttribArray(loc_col) glBindBuffer(GL_ARRAY_BUFFER, obj.VBO[1]) glVertexAttribPointer(loc_col, 4, GL_FLOAT, False, 0, ctypes.c_void_p(0)) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, obj.VBO[2]) glDrawElements(GL_TRIANGLES, obj.indices.size, GL_UNSIGNED_INT, ctypes.c_void_p(0)) glUseProgram(0) painter = QtGui.QPainter() painter.begin(self) painter.setRenderHint(QtGui.QPainter.Antialiasing) print(painter.isActive()) pen = QtGui.QPen(QtGui.QColor(255,0,0), 10) x = 100 y= 100 # clean previous drawings painter.fillRect(self.rect(),QtGui.QBrush(QtGui.QColor(0,0,0)) ) painter.setPen(pen) currentFont = painter.font() currentFont.setPointSize(currentFont.pointSize()*4) painter.setFont(currentFont) painter.drawLine(x, 0, x, self.height()) painter.drawLine(0, y, self.width(), y) painter.end() else: print('No data in objects ==> no drawing') painter = QtGui.QPainter() painter.begin(self) painter.setRenderHint(QtGui.QPainter.Antialiasing) print(painter.isActive()) pen = QtGui.QPen(QtGui.QColor(255,0,0), 10) x = 100 y= 100 # clean previous drawings painter.fillRect(self.rect(),QtGui.QBrush(QtGui.QColor(0,0,0)) ) painter.setPen(pen) currentFont = painter.font() currentFont.setPointSize(currentFont.pointSize()*4) painter.setFont(currentFont) painter.drawLine(x, 0, x, self.height()) painter.drawLine(0, y, self.width(), y) painter.end()