How To Import QtWebEngineWidgets using PyQt6

8,022 views
Skip to first unread message

tbp1...@gmail.com

unread,
Feb 13, 2022, 11:45:56 PM2/13/22
to leo-editor
Does anyone know how to import QtWebEngineWidgets with PyQt6?  As of version 6.23, this module exists, and I can import it (along with it QWebEngineView) in a standalone console that has not created a Qt Gui.

But when I try to import it in a script running in Leo, it fails:

from PyQt6 import QtWebEngineWidgets
exception executing script
ImportError: QtWebEngineWidgets must be imported before a QCoreApplication instance is created


Does this mean that Leo will need to import QtWebEngineWidgets before building its Qt GUI?  Has anyone here devised some way to get the import to work? I haven't found anything with on-line searches so far except that it isn't practical for a running application.

Otherwise, I can't see a way to import QWebEngineView in pyqt6, and it's needed for VR3 among others.

tbp1...@gmail.com

unread,
Feb 14, 2022, 10:29:55 AM2/14/22
to leo-editor
In a tiny toy program, if I imported QtWebEngineWidgets after creating the app and main window then the error was reproduced.  If I imported it before creating the App or main window, then the import succeeded, and QWebEngineView was available.

Edward K. Ream

unread,
Feb 14, 2022, 10:59:39 AM2/14/22
to leo-editor
On Mon, Feb 14, 2022 at 9:29 AM tbp1...@gmail.com <tbp1...@gmail.com> wrote:

In a tiny toy program, if I imported QtWebEngineWidgets after creating the app and main window then the error was reproduced.  If I imported it before creating the App or main window, then the import succeeded, and QWebEngineView was available.

I have just created #2415 for this. The mind boggles.

Edward

Edward K. Ream

unread,
Feb 14, 2022, 11:29:40 AM2/14/22
to leo-editor
On Monday, February 14, 2022 at 9:59:39 AM UTC-6 Edward K. Ream wrote:

> I have just created #2415 for this. The mind boggles.

PR #2416 (the ekr-web-engine branch) suggests the way forward.  At startup, for PyQt6 only, LeoApp.createQtGui imports QtWebEngineWidgets as follows:

from PyQt6 import QtWebEngineWidgets as qt6_QtWebEngineWidgets
self.qt6_QtWebEngineWidgets = qt6_QtWebEngineWidgets

 So instead of trying to import QtWebEngineWidgets, VR3 could just test and use g.app.qt6_QtWebEngineWidgets.

Does this work for you?

Edward

tbp1...@gmail.com

unread,
Feb 14, 2022, 11:55:42 AM2/14/22
to leo-editor
I have succeeded with a slightly different approach:

1.  In LeoQt6.py:

# Must import this before creating the GUI
try:
    from PyQt6 import QtWebEngineWidgets
except ImportError:
    print('No QtWebEngineWidgetsInstall with python3 -m pip install --upgrade ')


2.  Add a new try: block to existing VR3 code:

QWebView = None
if isQt6:
    try:
        from leo.core.leoQt6 import QtWebEngineWidgets
        QWebView = QtWebEngineWidgets.QWebEngineView
    except ImportError:
        if not g.unitTesting:
            g.trace("Can't import Qt6 QtWebEngineWidgets")
    except AttributeError:
        if not g.unitTesting:
            g.trace('No QWebView')
    except Exception as e:
        g.trace(e)
else:
    try:
        from leo.core.leoQt import QtWebKitWidgets
        QWebView = QtWebKitWidgets.QWebView
    except ImportError:
        if not g.unitTesting:
            g.trace("Can't import QtWebKitWidgets")
    except AttributeError:
        if not g.unitTesting:
            g.trace('No QWebView')
    except Exception as e:
        g.trace(e)


This gets VR3 to render as intended.  However, I'd like to see something in leoQt that makes it unnecessary to test for Qt6, but only for QtWebEngineWidgets. See next message for more on this.

tbp1...@gmail.com

unread,
Feb 14, 2022, 12:05:47 PM2/14/22
to leo-editor
As an alternative to the approach in my previous message,  Qt5 also has a QtWebEngineWidgets module.  It isn't used by current code because QWebView is actually a QWebEngineView under the covers.  But we could add importing  QtWebEngineWidgets for Qt5 as well as Qt6. Then leo.core.leoQt could return either the Qt5 or Qt6 version of QWebEngineView, no version tests required.  QWebView could be aliased to this QWebEngineView for use by existing code.

IMHO, this would be a better approach.  There will still be the problem that some of the Qt6 WebEngineView's enums have a different classpath, but we've dealt with that for other Qt classes already.

At any rate, any one of these three approaches gets VR3 and Freewin working again.  Hurray!

tbp1...@gmail.com

unread,
Feb 16, 2022, 9:38:13 AM2/16/22
to leo-editor
For those following this thread and who want to use the WebEngineView with PyQt6, my suggestion above has now made its way into the devel branch. You can look the the code for VR3 or Freewin (in LeoPyRef.leo) to see ways to import and use it.

Even though this lets you import the WebEngineView without needing to know whether Leo is using PyQt5 or 6, there are still changes with PyQt6 that could bite you, especially in the paths to some of the enums and constants.  So you may still need to have some version-conditional code paths to set them up.

tbp1...@gmail.com

unread,
Feb 16, 2022, 10:30:42 AM2/16/22
to leo-editor
Well, no need to look up how to get the WebEngineView.  Here it is:

from leo.core.leoQt import has_WebEngineWidgets
if has_WebEngineWidgets:
    from leo.core.leoQt import QtWebEngineWidgets
    QWebEngineView = QtWebEngineWidgets.QWebEngineView
    # If you have existing code that uses QWebView:
    QWebView =  QWebEngineView
Reply all
Reply to author
Forward
0 new messages