QWebEngineView not closing properly in some cases

I found that QWebEngineView behaves very strange (Windows PyQt5 with Qt 5.7.0) and in some cases does not finish properly leaving the application hanging. The following snippet works fine. But if you uncomment both lines marked with #1 and #2 and then try to close the application, the application get hung. The same happens if you uncomment only #3. I tried this with other widgets than QWebEngineView, and no problems occurred.

import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
app = QtWidgets.QApplication(sys.argv)
mainWindow = QtWidgets.QMainWindow()
widget = QtWebEngineWidgets.QWebEngineView()
# widget.load(QtCore.QUrl("http:/www.google.com"))  #1
mainWindow.setCentralWidget(widget)
# mainWindow.setCentralWidget(None)                 #2
# widget.deleteLater()                              #3
mainWindow.show()
result = app.exec_()
sys.exit(result)

This is the shortest code which can reproduce the error. But what I need in my much larger application is to set web engine view as the central widget and then with menu actions close it and set another widget as central widget. Any ideas how to fix the behaviour and achieve the expected behaviour? Or am I doing something wrong?

UPDATE:

I found even simpler example exhibiting the described issue. Running this on Windows, PyQt 5.7 leaves the application hanging.

import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
app = QtWidgets.QApplication(sys.argv)
widget = QtWebEngineWidgets.QWebEngineView()
widget.setAttribute(QtCore.Qt.WA_DeleteOnClose)
widget.show()
sys.exit(app.exec_())

UPDATE2: I create a ticket for this as this seems to me as a bug. https://bugreports.qt.io/browse/QTBUG-57228 However before the fix is delivered, any workaround would be very appreciated.

Upvotes: 1

Views: 2543

Answers (1)

Seems that I need to add del widget and del app just between result = app.exec_() and sys.exit(result). Then it works just as expected. Probably not an issue in QtWebEngine but in PyQt5 not properly releasing all references.

See comments for https://bugreports.qt.io/browse/QTBUG-57228

Upvotes: 3

Related Questions