Recently I have drawn simple scenes like triangles and polygon using PyOpenGL. The code was pretty straightforward, and use of different GL_TRIANGLES and GL_POLYGON didn't raise any questions.
After that I've decided to add GUI to my app and downloaded pyqt4. So now I'm using QtOpenGL from there, and I'm stuck. After reading several tutorials, the one thing I could perform was this. Here is the code: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_())
When trying to use, for example GL_TRIANGLES there, I have this error:
NameError: global name 'GL_TRIANGLES' is not defined
Maybe I haven't looked around enough, but I didn't find any solution.
So my question is how can I draw different figures inside my QGLWidget.
Thanks for your help.
Here's a simple working example for a python QGLWidget with a button