1

i'm trying to make PyQt5 and OpenGL working but cannot figure out what is missing. When i try to run this code i'm getting err 1282 invalid operation on glTransaltef(0.0, 0.0, -5) . I tried to google this error, but didn't found anything that involved this function.

app = QApplication(sys.argv)
window = mainWindow.mainWindow()
window.setupUI()
window.show()
sys.exit(app.exec_())

class mainWindow(QMainWindow):

    def __init__(self, *args):
        super(mainWindow, self).__init__(*args)
        loadUi('minimal.ui', self)

    def setupUI(self):
        self.openGLWidget.initializeGL()
        self.openGLWidget.resizeGL(651,551)
        gluPerspective(45, 651/551, 0.1, 50.0)
        glTranslatef(0.0,0.0, -5)

I'm using .ui file for my GUI layout, and it has openGLWidget object on it, which means ( if i got it correctly ) i don't have to declare QOpenGLWidget, because i already have one and all my OpenGL functions such as glTranslatef should take effect on what is displayed on this object.

haxscramper
  • 775
  • 7
  • 17
  • @eyllanesc Thanks, everythin works fine. In my code i did't import `OpenGL.GLUT` and forgot to redefine `openGLWIdget.paintGL` – haxscramper Jun 26 '17 at 16:28

1 Answers1

4

You must use the pyopengl library, and for your case the GLUT module, in addition to overriding the paintGl method, I show an example in the following part:

import sys

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.uic import *

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

class mainWindow(QMainWindow):

    def __init__(self, *args):
        super(mainWindow, self).__init__(*args)
        loadUi('minimal.ui', self)

    def setupUI(self):
        self.openGLWidget.initializeGL()
        self.openGLWidget.resizeGL(651,551)
        self.openGLWidget.paintGL = self.paintGL
        timer = QTimer(self)
        timer.timeout.connect(self.openGLWidget.update) 
        timer.start(1000)

    def paintGL(self):
        glClear(GL_COLOR_BUFFER_BIT)
        glColor3f(1,0,0);
        glBegin(GL_TRIANGLES);
        glVertex3f(-0.5,-0.5,0);
        glVertex3f(0.5,-0.5,0);
        glVertex3f(0.0,0.5,0);
        glEnd()

        gluPerspective(45, 651/551, 0.1, 50.0)
        glTranslatef(0.0,0.0, -5)



app = QApplication(sys.argv)
window = mainWindow()
window.setupUI()
window.show()
sys.exit(app.exec_())

The complete example can be found here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Why does the triangle model move away? Is this line responsible from the action? timer.timeout.connect(self.openGLWidget.update) If so where is the update method implemented? – Elif Feb 18 '21 at 10:58