haxscramper
haxscramper

Reputation: 775

Trying to get PyQt5 and OpenGL working

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.

Upvotes: 1

Views: 6557

Answers (1)

eyllanesc
eyllanesc

Reputation: 243907

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

Upvotes: 4

Related Questions