QWebEngineView content is not updated when it is displayed inside another widget
-
I used pyside2 and made a widget, inside which I added QWebEngineView. I wanted to call this widget wherever I wanted and pass the URL to it. When QWebEngineView is in an independent window or QStackedWidget, everything is fine. Still, when it is loaded in another widget, there is a problem, for example, when I type something, it shows the changes after I resize the window. It also has the same problem with scrolling and everything, and it doesn't show the changes until I resize the window.
I even used QDockWidget, it works fine when there is a popup, but as soon as I drag it to the window behind it and it sticks, the same problem occurs again.this is my code:
from PySide2.QtCore import Qt, QUrl from PySide2.QtGui import QDesktopServices from PySide2.QtWidgets import QGraphicsView, QGraphicsScene, QGraphicsProxyWidget, QSizePolicy, QGraphicsOpacityEffect from PySide2.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings, QWebEnginePage class CustomWebEnginePage(QWebEnginePage): external_windows = [] def acceptNavigationRequest(self, url, _type, isMainFrame): if _type == QWebEnginePage.NavigationTypeLinkClicked: QDesktopServices.openUrl(url) return False return super().acceptNavigationRequest(url, _type, isMainFrame) class WebViewWidget(QGraphicsView): def __init__(self, parent=None): super().__init__(parent) self.scene = QGraphicsScene(self) self.setStyleSheet("*{border: 0px;background:#44475a;}") self.webview = QWebEngineView() self.webview.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.webview.setPage(CustomWebEnginePage(self)) self.webview.parent() self.webview.loadFinished.connect(self.on_load_finished) self.opacity_effect = QGraphicsOpacityEffect() self.webview.setGraphicsEffect(self.opacity_effect) self.opacity_effect.setOpacity(0.0) self.webview.settings().setAttribute(QWebEngineSettings.ShowScrollBars, False) self.proxy = QGraphicsProxyWidget() self.proxy.setWidget(self.webview) self.scene.addItem(self.proxy) self.setScene(self.scene) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.webview.setContextMenuPolicy(Qt.NoContextMenu) def set_url(self, url): self.webview.setUrl(QUrl(url)) def resizeEvent(self, event): self.changeSize() def on_load_finished(self, success): if success: self.verticalScrollBar().setSliderPosition(0) self.webview.setVisible(True) self.changeSize() self.opacity_effect.setOpacity(1.0) def changeSize(self): self.webview.setFixedSize(self.size()) self.showMaximized()
I should also mention that I used the following template in my code, which may interfere with QWebEnginePage:
-
@nekooee said in QWebEngineView content is not updated when it is displayed inside another widget:
I should also mention that I used the following template in my code, which may interfere with QWebEnginePage:
So how does it behave when you tried it without this additional stuff?
-
@JonB When it opens in a new window that has a title bar (not used template), there is no problem. The template that I used has removed the title bar. I think there is a problem with the code of this template, but the author of the template says that the problem is not related to his project!
See the discussion below:
https://github.com/Wanderson-Magalhaes/Simple_PySide_Base/issues/12