[Python] Repeatedly open/interact with/close PyQt6.QtWebEngineWidgets.QWebEngineView widgets

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    EDIT: I reformulated my question in a hopefully clearer way. Please see: Repeatedly create/delete a PyQt6.QtWebEngineWidgets.QWebEngineView widget


    I would like to open/interact_with a PyQt6.QtWebEngineWidgets.QWebEngineView widget, then close it, then open/interact_with a new one... each time using a new QApplication (it is important that, once done using a given QWebEngineView instance, there should be no QApplication instance to avoid interference with other GUI toolkits). Here is a toy example to illustrate my goal:

    # New app in each loop / QWebEngineView => crashes
    import sys
    from PyQt6.QtWebEngineWidgets import QWebEngineView as widget_t
    from PyQt6.QtWidgets import QApplication as application_t

    html = """
    <!DOCTYPE html>
    <html>
    <body>
    <h1>Content Title</h1>
    <p>Content.</p>
    </body>
    </html>
    """

    for _ in range(2):
    application = application_t(sys.argv)

    widget = widget_t()
    widget.setHtml(html)
    widget.show()

    application.exec()
    del application # Found in a discussion that this was necessary.


    This sample code crashes (in Qt) during the second iteration. (I read many discussions about properly closing a QWebEngineView, and did some attempts to follow the recommendations.) What seems to be the problem is the combination QWebEngineView+"new QApplication". If I use a QLabel, it works:

    # New app in each loop / QLabel => works
    import sys
    from PyQt6.QtWidgets import QLabel as widget_t
    from PyQt6.QtWidgets import QApplication as application_t

    for _ in range(2):
    application = application_t(sys.argv)

    widget = widget_t("Content")
    widget.show()

    application.exec()
    del application # Found in a discussion that this was necessary.


    If I use the same QApplication in each loops, it also works:

    # Same app in each loop / QWebEngineView => works
    import sys
    from PyQt6.QtWebEngineWidgets import QWebEngineView as widget_t
    from PyQt6.QtWidgets import QApplication as application_t

    html = """
    <!DOCTYPE html>
    <html>
    <body>
    <h1>Content Title</h1>
    <p>Content.</p>
    </body>
    </html>
    """

    application = application_t(sys.argv)
    for _ in range(2):
    widget = widget_t()
    widget.setHtml(html)
    widget.show()

    application.exec()


    How can I transform the "New app in each loop / QWebEngineView => crashes" code above into a "New app in each loop / QWebEngineView => works" version?

    Continue reading...

Compartilhe esta Página