PyQtWebEngine qwebengineview - PreInitialisationCode does not take into account Qt::AA_ShareOpenGLContexts attribute

Jean-Baptiste Peter jbpeter at outlook.com
Tue Mar 15 09:00:39 GMT 2022


Hi,

In some cases, it is not possible to import QtWebEngineWidgets before a QCoreApplication instance is created. In that case, Qt provides an alternative by setting Qt::AA_ShareOpenGLContexts attribute (QT doc for reference: https://doc.qt.io/qt-5/qtwebengine.html). However, PyQtWebEngine currently only check in the PreInitialisationCode if a QCoreApplication instance has been created and throw the following error if that's the case :

ImportError "QtWebEngineWidgets must be imported before a QCoreApplication instance is created"



Proposed solution :

Adding the following check in PyQtWebEngine\sip\QtWebEngineWidgets\qwebengineview.sip (from line 194) solved this problem for me :


%PreInitialisationCode
// QtWebEngineWidgets uses Q_COREAPP_STARTUP_FUNCTION so, in order to make sure
// things work (particularly on Windows) when we dynamically load the code, we
// check things have been done in the right order.
if (QCoreApplication::instance())
{
if (!QCoreApplication::testAttribute(Qt::AA_ShareOpenGLContexts))
{
  PyErr_SetString(PyExc_ImportError,"QtWebEngineWidgets must be imported or Qt::AA_ShareOpenGLContexts must be set before a QCoreApplication instance is created");
  SIP_MODULE_RETURN(NULL);
}
}
%End

I tested this solution with PyQtWebEngine 5.15.5 on windows 11 and this works as expected in my use case. The problem is most likely the same with PyQt6-WebEngine 6.2.1 as QtWebEngineWidgets works the same way in QT 6 but I have not been able to test with this version.

Best,

Jean-Baptiste Peter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20220315/d23f863a/attachment.htm>


More information about the PyQt mailing list