Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. QWebEngineView closes/reopens window when added dynamically

QWebEngineView closes/reopens window when added dynamically

Scheduled Pinned Locked Moved Solved QtWebEngine
pysideqwebengineview
9 Posts 3 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    somethingvague
    wrote on 11 Dec 2022, 04:17 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      somethingvague
      wrote on 11 Dec 2022, 04:40 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        somethingvague
        wrote on 12 Dec 2022, 15:31 last edited by
        #3

        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())
        
        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 12 Dec 2022, 19:50 last edited by
          #4

          Hi,

          I am wondering whether you are experiencing the OpenGL switch flicker described in this example.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            somethingvague
            wrote on 12 Dec 2022, 19:57 last edited by somethingvague 12 Dec 2022, 19:58
            #5

            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?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 12 Dec 2022, 20:00 last edited by
              #6

              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 ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • S Offline
                S Offline
                somethingvague
                wrote on 12 Dec 2022, 20:08 last edited by
                #7

                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.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  somethingvague
                  wrote on 12 Dec 2022, 23:57 last edited by
                  #8

                  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.

                  A 1 Reply Last reply 3 Jun 2024, 05:53
                  1
                  • S somethingvague
                    12 Dec 2022, 23:57

                    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.

                    A Offline
                    A Offline
                    AnujPandey
                    wrote on 3 Jun 2024, 05:53 last edited by
                    #9

                    @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?

                    ffb38eee-739e-4e23-b32f-7af6e8fb171d-image.png

                    30f649a4-679d-4b50-8bf0-24a6c6155131-image.png

                    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.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved