1

In my application I have a list of buttons and clicking on each creates a new QWebEngineView in a QMainWindow. On closing the QMainWindow I want the QWebEngineView (and the QWebEngineProcess that it spawns) to be destroyed. But this does not happen - if I click on multiple buttons, multiple QWebEngineProcesses are created that eats up memory. Why are these processes not getting killed even after destroying the web view? Can anyone please help me with this?

enter image description here

Here is a sample code that I tried while trying to solve this problem. I have a single button here and clicking on it each time creates a new main window with a QWebEngineView.

import sys
from PySide6.QtWidgets import (QApplication, QWidget, QVBoxLayout, QPushButton, QMainWindow)
from PySide6 import QtCore
from PySide6.QtWebEngineWidgets import QWebEngineView


class PlotWindow(QMainWindow):
    def __init__(self, parent):
        """Constructor."""
        super(PlotWindow, self).__init__(parent)
        self.webV = QWebEngineView()
        self.webV.load('https://en.wikipedia.org/')
        self.setCentralWidget(self.webV)


    def closeEvent(self, event):
        self.webV.destroy() # => This does not kill background process QWebEngineProcess



class webView(QWidget):
    def __init__(self):
        super(webView, self).__init__()

        self.layout = QVBoxLayout(self)
        btn = QPushButton('Click')
        btn.clicked.connect(lambda: self.create_plot(self))
        self.layout.addWidget(btn)

        self.setLayout(self.layout)

    @QtCore.Slot()
    def create_plot(self, widget):
        win = PlotWindow(widget)
        win.show()


if __name__ == "__main__":
    app = QApplication()

    web = webView()
    web.show()

    sys.exit(app.exec())

I came across this : https://doc.qt.io/qtforpython-6/overviews/qtwebengine-features.html#process-models and "Process Per Site" might be a possible solution for this problem, but I really do not understand how and where to use the command line arguments. Just passing them as args to QApplication() doesn't work.

mahkitah
  • 562
  • 1
  • 6
  • 19
Lizzzz90
  • 125
  • 1
  • 1
  • 8
  • 1
    First of all, use `deleteLater()`, not `destroy()`, then you should delete the whole window, not just the view. That should theoretically take care of any remaining reference. If that's not enough, try to delete the page as well, but that should theoretically not happen (unless you're keeping references somewhere else) – musicamante Apr 28 '23 at 20:27
  • deleteLater() works! it removes the QtWebEngineProcess. Thank you @musicmante – Lizzzz90 Apr 29 '23 at 19:19
  • Note that you can avoid all that (including overriding `closeEvent()`) just by setting the `WA_DeleteOnClose` attribute on the window. See [`QWidget.setAttribute()`](https://doc.qt.io/qt-5/qwidget.html#setAttribute). – musicamante Apr 29 '23 at 22:08
  • The attribute does not work for some reason for me. I added self.setAtrribute(QtCore.Qt.WA_DeleteOnClose, True) in class PlotWindow that inherits QMainWindow – Lizzzz90 May 01 '23 at 19:08
  • Please explain what you mean by "does not work". – musicamante May 01 '23 at 23:18
  • I removed the closeEvent override and added the setAttribute as mentioned above. But that did not remove the QtWebEngineProcess on closing the window. – Lizzzz90 May 02 '23 at 12:06

0 Answers0