PyQt OpenGL: drawing simple scenes
23,199
Solution 1
Here's a simple working example for a python QGLWidget with a button:
from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt4 import QtGui
from PyQt4.QtOpenGL import *
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.widget = glWidget(self)
self.button = QtGui.QPushButton('Test', self)
mainLayout = QtGui.QHBoxLayout()
mainLayout.addWidget(self.widget)
mainLayout.addWidget(self.button)
self.setLayout(mainLayout)
class glWidget(QGLWidget):
def __init__(self, parent):
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 = QtGui.QApplication(['Yo'])
window = MainWindow()
window.show()
app.exec_()
Solution 2
I modify Ivan's answer for Pyqt version 5.
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(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Ui_MainWindow, self).__init__()
self.widget = glWidget()
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)
ui.show()
sys.exit(app.exec_())
Comments
-
a-a over 4 years
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.
-
a-a over 8 yearsMuch appreciated, Mr Whale!
-
Mafster over 4 yearsThis instantly closes. Fixed it by adding sys.exit(app.exec_()) in main condition at bottom
-
panofish over 3 yearsWhy does my opengl view show only black and I don't see anything?
-
Cloud Cho over 3 years@panofish, my guess are color selection, Window size, or zoomed in too much. Have you tried my snippet with given values?
-
panofish over 3 yearsI ran your snippet exactly as shown... it ran without errors but the opengl widget shows only black. I am running PyQt5 and Python 3.7
-
Cloud Cho over 3 years@panofish Do you mind open new question with your code? If you add some more details, I or other one could find causes by reproducing your error.
-
panofish over 3 yearsAppreciate the suggestion. The code is exactly the same as you have shown here. No changes. Just using Python 3.7.
-
Cloud Cho over 3 years@panofish, please share your OpenGL version.
-
panofish over 3 yearsaccording to GLView free tool... my opengl version is 4.6