QWebEngineView closes/reopens window when added dynamically
-
I have an application with a QStackedWidget. One of the widgets that gets added to the stack has a QWebEngineView in its layout to display PDFs. When that widget is created dynamically and added to the stack, the entire application window closes and then reopens. Granted, when the window reopens, the dynamically created widget has the QWebEngineView where it should be and the PDF displaying correctly, but why is the window closing and then reopening?
-
I should add that if I delete the widget with the QWebEngineView embedded, re-create it and re-add it, the application window does not close and reopen. It seems the window only does this the first time a QWebEngineView is embedded anywhere and then it's fine after that.
-
Here is a simple example that shows the behavior. You just need to supply it with a path to a PDF. When you run it, you should see it close immediately, and then reopen with the PDF. I'm getting pretty desperate, if anyone can verify this behavior and let me know if there's a way around it. Thanks.
from PySide6.QtCore import QUrl from PySide6.QtWidgets import QApplication, QMainWindow from PySide6.QtWebEngineWidgets import QWebEngineView from os import path import sys class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setGeometry(0, 28, 1000, 750) self.webView = QWebEngineView() self.webView.settings().setAttribute(self.webView.settings().WebAttribute.PluginsEnabled, True) self.webView.settings().setAttribute(self.webView.settings().WebAttribute.PdfViewerEnabled, True) self.setCentralWidget(self.webView) self.show() if __name__ == '__main__': app = QApplication(sys.argv) win = MainWindow() win.webView.setUrl(QUrl(### PDF PATH HERE ###)) sys.exit(app.exec())
-
Hi,
I am wondering whether you are experiencing the OpenGL switch flicker described in this example.
-
You may be correct. In the article you posted, it suggested putting the .show() after loading the content. In my example, taking it from
if __name__ == '__main__': app = QApplication(sys.argv) win = MainWindow() win.show() win.webView.setUrl(QUrl(### PDF URL ###)) sys.exit(app.exec())
to
if __name__ == '__main__': app = QApplication(sys.argv) win = MainWindow() win.webView.setUrl(QUrl(### PDF URL ###)) win.show() sys.exit(app.exec())
That fixes the issue in my example. But the question is how to fix it in my application, where the app is already loaded and existing before any QWebEngineView's are created?
-
What if you do a "dummy" init with a view that you would delete right after ?
Is there a chance that no web view is created during the lifetime of your application ?
-
I'm not sure how to go about that. In the init of the application, I created a dummy view
pdf_view = QWebEngineView() pdf_view.setUrl(QUrl(''))
and then the self.show() comes after it. But unless I actually put it in the layout somewhere, it still flickers the app when the real use is loaded later. And if I do put that empty view in the layout before the self.show() in the init method, it still flickers the app, actually knocking it out of fullscreen, which is weird. Not sure why the behavior is different than the simple example I included above.
-
I figured out what was keeping the above fix for working with my app. I was using the showMaximized() command before creating the QWebEngineView. Turns out I have to create the view first and put it in the layout (I then hide it), then maximize the window, then show it. Seems like I shouldn't have to do all of that to keep the window from flickering, but I'll take it.
-
@somethingvague I'm also having the same problem with my application. In my case I initialize the class which contains the web engine view instance (let us say class B) after someone clicks some button in a different class(let's say A), Now both classes A and B have different UI interfaces. Can you please help me stop flicker from happening in this case?
I have shown the Class B initialization and engine setup above.
I have initialized class and enigine setup before window.show() as been told in the above conversation.