且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

试图让PyQt5和OpenGL工作

更新时间:2023-11-30 16:01:34

除了覆盖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_())

完整的示例可以在此处