[PyQt] Programs that need opengl won't run.

Erik Hvatum ice.rikh at gmail.com
Wed Jul 23 21:00:49 BST 2014


Firstly, I have had trouble with OpenGL_accelerate from time to time - I
would try uninstalling OpenGL_accelerate while keeping plain pyopengl to
see if that helps anything.

I think QOpenGLFunctions in PyQt is incomplete simply because there are a
heck of a lot of OpenGL functions and many of them want void* blobs that
would take significant wrapping and infrastructure to present in a sensible
way, which PyOpenGL already does tolerably.

That said, it is possible to use the new Qt5 QWindow and QOpenGLContext
stuff together with PyOpenGL in Python on an OpenGL 2.0 context.  Here's a
little example I threw together doing that:

#!/usr/bin/env python3

from OpenGL import GL
from PyQt5 import Qt

class GlQWindow(Qt.QWindow):
    def __init__(self):
        super().__init__()
        format = Qt.QSurfaceFormat()
        format.setVersion(2, 0)
        format.setProfile(Qt.QSurfaceFormat.CompatibilityProfile)
        format.setStereo(False)
        format.setSwapBehavior(Qt.QSurfaceFormat.DoubleBuffer)
        self.setSurfaceType(Qt.QWindow.OpenGLSurface)
        self.context = Qt.QOpenGLContext()
        self.context.setFormat(format)
        if not self.context.create():
            raise Exception('self.context.create() failed')
        self.create()

    def exposeEvent(self, ev):
        ev.accept()
        if self.isExposed() and self.isVisible():
            self.update()

    def makeCurrent(self):
        self.context.makeCurrent(self)

    def swapBuffers(self):
        self.context.swapBuffers(self)

    def update(self):
        self.makeCurrent()
        GL.glClearColor(0,0,0,0)
        GL.glClearDepth(1)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        GL.glBegin(GL.GL_TRIANGLES)
        GL.glVertex2f(0.5, 1)
        GL.glVertex2f(1, 0)
        GL.glVertex2f(0, 0)
        GL.glEnd()
        GL.glFlush()
        self.swapBuffers()


app = Qt.QApplication([])
glQWindow = GlQWindow()
glQWindowWidget = Qt.QWidget.createWindowContainer(glQWindow, None,
Qt.Qt.Widget)
glQWindowWidget.show()
app.exec_()

This wasn't exactly the easiest to figure out, but it works quite well, and
it can be extended to support drawing on the OpenGL context from a
different thread by doing
glQWindow.context.moveToThread(a_qthread_instance).
QOpenGLContext.swapBuffers(..) is thread safe, FYI, or is at least safe to
call from the thread owning the context.

Incidentally, if your Qt binaries were compiled with OpenGL ES support
only, you're going to want to rebuild them with *desktop* OpenGL support
enabled.  I mention this only because I went through a period of total
confusion as a result of having ES only binaries.  The official binaries
from qt-project.org are all built with desktop OpenGL support, except for
Android and iOS, of course.

-Erik

On Wed, Jul 23, 2014 at 12:15 PM, Alan Ezust <alan.ezust at gmail.com> wrote:

> I am trying to do openGL on a virtual machine, which only supports OpenGL
> 2.1
> I've checked that my directX version is 11
>
> Most of of the PyQt examples don’t work because (I think) our openGL is
> only 2.1 on the virtual machines.
> However, openGLWindow.py works fine, and spits out no errors.
>
> The other examples run but spits out lots of errors, and I can’t see
> anything in the windows.
> Here is the output of grabber.py:
>
> C:\Python33\python.exe
> C:/Python33/Lib/site-packages/PyQt5/examples/opengl/grabber.py
> Traceback (most recent call last):
>   File "C:/Python33/Lib/site-packages/PyQt5/examples/opengl/grabber.py",
> line 114, in initializeGL
>     glLightfv(GL_LIGHT0, GL_POSITION, lightPos)
>   File "latebind.pyx", line 32, in
> OpenGL_accelerate.latebind.LateBind.__call__
> (c:\Users\mcfletch\OpenGL-dev\OpenGL-ctypes\OpenGL_accelerate\src\latebind.c:989)
>   File "wrapper.pyx", line 318, in
> OpenGL_accelerate.wrapper.Wrapper.__call__
> (c:\Users\mcfletch\OpenGL-dev\OpenGL-ctypes\OpenGL_accelerate\src\wrapper.c:6561)
>   File "wrapper.pyx", line 311, in
> OpenGL_accelerate.wrapper.Wrapper.__call__
> (c:\Users\mcfletch\OpenGL-dev\OpenGL-ctypes\OpenGL_accelerate\src\wrapper.c:6439)
>   File "C:\Python33\lib\site-packages\OpenGL\platform\baseplatform.py",
> line 402, in __call__
>     return self( *args, **named )
>   File "errorchecker.pyx", line 53, in
> OpenGL_accelerate.errorchecker._ErrorChecker.glCheckError
> (c:\Users\mcfletch\OpenGL-dev\OpenGL-ctypes\OpenGL_accelerate\src\errorchecker.c:1218)
> OpenGL.error.GLError: GLError(
>     err = 1282,
>     description = b'invalid operation',
>     baseOperation = glLightfv,
>     pyArgs = (
>         GL_LIGHT0,
>         GL_POSITION,
>         <OpenGL.raw.GL._types.c_float_Array_4 object at 0x03227800>,
>     ),
>     cArgs = (
>         GL_LIGHT0,
>         GL_POSITION,
>         <OpenGL.raw.GL._types.c_float_Array_4 object at 0x03227800>,
>     ),
>     cArguments = (
>         GL_LIGHT0,
>         GL_POSITION,
>         <OpenGL.raw.GL._types.c_float_Array_4 object at 0x03227800>,
>     )
> )
>
> 2dpainting.py, helloGL.py and overpainting.py do the same thing as
> grabber.py
>
> The main difference with openGLWindow compared to the other examples is
> that it uses Qt’s initializeOpenGLFunctions() to get access to openGL API
> of the correct version. This seems to be safer across multiple platforms
> than using OpenGL Apis directly, as the other examples do.
>
>        if needsInitialize:
>             self.m_gl = self.m_context.versionFunctions()
>             self.m_gl.initializeOpenGLFunctions()
>
> Do the other examples even check to see what version of openGL is running?
>
> Is there any reason why QOpenGLFunctions is not available from PyQt?
>
>
>
>
> On Sun, Mar 2, 2014 at 4:41 AM, Matthew Ngaha <chigga101 at gmail.com> wrote:
>
>> Hey David. I've updated and re-updated the drivers to no avail. But
>> what really puzzles me is my card is Intel (R) HD graphics. My mum's
>> PC uses an earlier version of the same driver and Qt5 programs run
>> fine. But mine a more recent version doesn't work. I now realise it's
>> not a Qt problem, maybe I can contact Intel's support desk.
>>
>> Thanks for heloing.
>> _______________________________________________
>> PyQt mailing list    PyQt at riverbankcomputing.com
>> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>>
>
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140723/4d7435ce/attachment.html>


More information about the PyQt mailing list