PyQtWebEngine qwebengineview - PreInitialisationCode does not take into account Qt::AA_ShareOpenGLContexts attribute
Phil Thompson
phil at riverbankcomputing.com
Tue Mar 15 17:13:05 GMT 2022
On 15/03/2022 09:00, Jean-Baptiste Peter wrote:
> 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
Applied to PyQt5 and PyQt6.
Thanks,
Phil
More information about the PyQt
mailing list