PyQt OpenGL:绘制简单的场景

时间:2015-10-18 18:03:10

标签: python qt opengl pyqt pyqt4

最近我使用PyOpenGL绘制了三角形和多边形等简单场景。代码非常简单,使用不同的GL_TRIANGLES和GL_POLYGON没有提出任何问题。

之后我决定将GUI添加到我的应用程序并下载pyqt4。 所以现在我从那里使用QtOpenGL,我被卡住了。 阅读了几篇教程之后,我能做的一件事就是this。这是代码:x

import sys
import math

from PyQt4 import QtCore, QtGui, QtOpenGL

try:
    from OpenGL import GL
except ImportError:
    app = QtGui.QApplication(sys.argv)
    QtGui.QMessageBox.critical(None, "OpenGL hellogl",
            "PyOpenGL must be installed to run this example.")
    sys.exit(1)


class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()

        self.glWidget = GLWidget()
        self.button = self.createButton()

        mainLayout = QtGui.QHBoxLayout()
        mainLayout.addWidget(self.glWidget)
        mainLayout.addWidget(self.button)

        self.setLayout(mainLayout)

        self.setWindowTitle("Hello GL")
    def createButton(self):
        button = QtGui.QPushButton("&WOOF")
        button.clicked.connect(self.close)
        return button



class GLWidget(QtOpenGL.QGLWidget):
    def __init__(self, parent=None):
        super(GLWidget, self).__init__(parent)

        self.trolltechPurple = QtGui.QColor.fromCmykF(0.39, 0.39, 0.0, 0.0)

    def minimumSizeHint(self):
        return QtCore.QSize(100, 300)

    def sizeHint(self):
        return QtCore.QSize(400, 400)


    def initializeGL(self):
        self.qglClearColor(self.trolltechPurple.dark())

    def paintGL(self):
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glLoadIdentity()
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        GL.glColor3f(1,0,0)
        GL.glRectf(-1,-1,1,0)
        GL.glColor3f(0,1,0)
        GL.glRectf(-1,0,1,1)
        GL.glBegin(GL_TRIANGLES)
        glVertex2f(3.0, 3.0)
        glVertex2f(5.0, 3.0)
        glVertex2f(5.0, 5.0)
        glVertex2f(6.0, 4.0)
        glVertex2f(7.0, 4.0)
        glVertex2f(7.0, 7.0)
        glEnd()
        GL.glFinish()



if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

当尝试使用时,例如GL_TRIANGLES,我有这个错误:

NameError: global name 'GL_TRIANGLES' is not defined

也许我没有四处寻找,但我找不到任何解决方案。

所以我的问题是如何在QGLWidget中绘制不同的数字。

感谢您的帮助。

2 个答案:

答案 0 :(得分:6)

这是一个带有按钮

的python QGLWidget的简单工作示例
 curl -PUT "http://localhost:9200/blabla/blabla/_bulk?pretty" -d '
{"create": {}}\n
{"name" : "super"}\n
' -i

答案 1 :(得分:2)

我修改了Pyqt版本5的Ivan答案。

import sys
from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt5 import QtGui
from PyQt5.QtOpenGL import *
from PyQt5 import QtCore, QtWidgets, QtOpenGL


class Ui_MainWindow(QtOpenGL.QGLWidget):
    def __init__(self, parent=None):
        super(Ui_MainWindow, self).__init__()
        self.widget = QtWidgets.QOpenGLWidget(self)
        self.button = QtWidgets.QPushButton('Test', self)
        mainLayout = QtWidgets.QHBoxLayout()
        mainLayout.addWidget(self.widget)
        mainLayout.addWidget(self.button)
        self.setLayout(mainLayout)


class glWidget(QGLWidget):
    def __init__(self, parent=None):
        QGLWidget.__init__(self, parent)
        self.setMinimumSize(640, 480)

    def paintGL(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()
        glTranslatef(-2.5, 0.5, -6.0)
        glColor3f( 1.0, 1.5, 0.0 );
        glPolygonMode(GL_FRONT, GL_FILL);
        glBegin(GL_TRIANGLES)
        glVertex3f(2.0,-1.2,0.0)
        glVertex3f(2.6,0.0,0.0)
        glVertex3f(2.9,-1.2,0.0)
        glEnd()
        glFlush()

    def initializeGL(self):
        glClearDepth(1.0)              
        glDepthFunc(GL_LESS)
        glEnable(GL_DEPTH_TEST)
        glShadeModel(GL_SMOOTH)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()                    
        gluPerspective(45.0,1.33,0.1, 100.0) 
        glMatrixMode(GL_MODELVIEW)


if __name__ == '__main__':    
    app = QtWidgets.QApplication(sys.argv)    
    Form = QtWidgets.QMainWindow()
    ui = Ui_MainWindow(Form)    
    viewer = Ui_MainWindow()
    viewer.show()    
    sys.exit(app.exec_())