Force Windows to use OpenGL with Qt6

In Qt6 Qt has changed the default renderer for Qt windows in MS Windows to use Direct3D instead of OpenGL. FreeCAD requires that the window be rendered with OpenGL, and if it isn’t then when we add our QOpenGLWidget (the MDIView) to the window, the program crashes with the error:

The top-level window is not using OpenGL for composition, 'D3D11' is not compatible with QOpenGLWidget

I can work around this by setting a Windows environment variable,

QSG_RHI_BACKEND=opengl

but that’s pretty awkward, and I don’t know how to make sure that happens for our users (something in the installer?). Does anyone know of any other way to force Qt6 to use OpenGL when rendering on Windows?

According to the Qt6 docs, you can execute the following line early in main():

QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);

Edit: I just realized that this is only for Qt Quick. I haven’t found a solution for normal Qt.

Do we have now a LibPack with Qt6?

Very close! (I think…) I’m still fighting with a few random crashes. I hope to push an alpha version this next week, but it’s not quite ready for primetime yet. It’s more than just Qt6, I wanted to update the whole stack, including CLBundler itself (which was Python 2 code).

Have you tried to do:

        qputenv("QT3D_RENDERER", "opengl");
        qputenv("QSG_RHI_BACKEND", "opengl");

This is what is done in some QT examples: https://github.com/qt/qtdatavis3d/blob/89a18d4c645aedd7c9cb0b787e8a308eec72d8e8/src/datavisualization/doc/snippets/doc_src_q3dbars_construction.cpp#L9 and it is also mentioned in the “What’s new” for QT 6.1: https://doc.qt.io/qt-6/whatsnew61.html#qt-data-visualization-module

Could it be that for this Qt6 build OpenGL is loaded dynamically? https://doc.qt.io/qt-6/windows-graphics.html

Now that Qt is switching, is there any chance to get rid of OpenGL? I am in sorrow because Apple announced already to not support it any longer.

Today I have created a Qt6 build with my MSYS2 build environment and there it works out of the box to create a document. Does this tell us something?

Not in the short term, coin3d requires OpenGL. So to leave OpenGL behind we would have to swap out coin3d, which is a big endeavour. Realthunder has been working on using a new renederer for some time, if I’m not mistaken that one is compatible with vullkan, d3d and metal. I don’t know what’s the status of that, I think there’s a few things that still don’t work.

I do believe that is something we should do in the long term, manufacturers don’t seem to pay much attention to openGL nowadays and many mobile chips (which are becoming more relevant every year) don’t even have regular desktop openGL support but only openGL ES, which is not compatible with coin3d either.