YOU DON'T. Also, it's not PyQt5's fault; the following C++ code also causes the same crash for the same reason:
#include
#include
#include
class GlWidget : public QOpenGLWidget
{
public:
GlWidget()
{
QSurfaceFormat f;
f.setRenderableType(QSurfaceFormat::OpenGL);
f.setVersion(4,1);
f.setProfile(QSurfaceFormat::CoreProfile);
f.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
f.setStereo(false);
setFormat(f);
}
virtual void initializeGL()
{
}
virtual void paintGL()
{
}
virtual void resizeGL(int w, int h)
{
}
};
int main(int argc, char** argv)
{
QApplication app(argc, argv);
GlWidget* glw = new GlWidget();
glw->show();
return app.exec();
}
Here is what the crash looks like (stack trace from the offending thread):
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libobjc.A.dylib 0x00007fff848a30dd objc_msgSend + 29
1 libqcocoa.dylib 0x00000001129839d3 QCocoaIntegration::createPlatformOpenGLContext(QOpenGLContext*) const + 83
2 org.qt-project.QtGui 0x000000010fce471a QOpenGLContext::create() + 74
3 org.qt-project.QtWidgets 0x000000010f65304d QOpenGLWidgetPrivate::initialize() + 141
4 org.qt-project.QtWidgets 0x000000010f653e1a QOpenGLWidget::resizeEvent(QResizeEvent*) + 58
5 org.qt-project.QtWidgets 0x000000010f6334e2 QWidget::event(QEvent*) + 2162
6 org.qt-project.QtWidgets 0x000000010f5f3dbb QApplicationPrivate::notify_helper(QObject*, QEvent*) + 251
7 org.qt-project.QtWidgets 0x000000010f5f7110 QApplication::notify(QObject*, QEvent*) + 8192
8 org.qt-project.QtCore 0x0000000110475fb3 QCoreApplication::notifyInternal(QObject*, QEvent*) + 115
9 org.qt-project.QtWidgets 0x000000010f62bf17 QWidgetPrivate::sendPendingMoveAndResizeEvents(bool, bool) + 327
10 org.qt-project.QtWidgets 0x000000010f631c1a QWidgetPrivate::show_helper() + 42
11 org.qt-project.QtWidgets 0x000000010f632a42 QWidget::setVisible(bool) + 1394
12 qt5glcrashtest 0x000000010f552963 main + 67
13 qt5glcrashtest 0x000000010f552914 start + 52
Three important things to note:
- On OS X, with OpenGL compatibility profile, which allows calls deprecated by OpenGL 3, you are strictly limited to OpenGL 2.1
- The QPainter related stuff supported by the widget-ness of QOpenGLWidget relies on deprecated OpenGL calls
- In Qt5 (the logic responsible may reside at a lower level) on OS X, specifically requesting an OpenGL 4.1 context with compatibility profile results in an OpenGL 2.1 context. Specifically requesting 4.1 with core profile does result in a 4.1 context - one that does not support the deprecated calls.
In short, it's no surprise that QOpenGLWidget with a 4.1 core profile context on OS X fails in some way. The workaround is to chose to either use QOpenGL
Widget with OpenGL 2.1 or QOpenGL
Window with OpenGL 4.1. This deprives you of direct QPainter support, but it does not prevent you from making your own QPainter that draws to a buffer you then composite via OpenGL in your QOpenGLWindow-derived object. NB: if you want to make your QOpenGLWindow-derived object's window a child of a normal Qt widget that can be placed by the QLayout classes, you'll want to do something along the lines of:
- With PyQt5, glwidget = Qt.QWidget.createWindowContainer(glwindow, None, Qt.Qt.Widget)
- In C++: glwidget = QWidget::createWindowContainer(glwindow, parent, Qt::Widget);
Incidentally, if you need to support OpenGLWindow with OpenGL 4.3 on OS X, then I have bad news for you. OS X does not support OpenGL 4.3. Apple makes approximately all of their money on iOS trash. Any money spent on OS X development is, by comparison, completely wasted. Don't hold your breath waiting for them to improve OS X. Instead, expect half-baked iOS features to appear in OS X quarter-baked. That is, at best, OS X might support some new OpenGL ES features, with a huge number of wont-fix bugs. Too bad, so sad, get a real OS.