试图让PyQt5和OpenGL工作 [英] Trying to get PyQt5 and OpenGL working

查看:841
本文介绍了试图让PyQt5和OpenGL工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使PyQt5和OpenGL正常工作,但无法找出缺少的内容.当我尝试运行此代码时,我在glTransaltef(0.0, 0.0, -5)上获得了err 1282 invalid operation.我尝试通过Google搜索此错误,但未找到与此功能有关的任何内容.

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)

我在GUI布局中使用.ui文件,并且它上面有openGLWidget对象,这意味着(如果我正确输入的话)我不必声明QOpenGLWidget,因为我已经拥有一个和所有我的OpenGL glTranslatef之类的功能 应该会对该对象上显示的内容生效.

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.

推荐答案

除了覆盖paintGl方法之外,您还必须使用pyopengl库以及GLUT模块(在您的情况下),在以下部分中显示示例:

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_())

完整的示例可以在此处

这篇关于试图让PyQt5和OpenGL工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆