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 - Application Not Responding

QWebEngineView - Application Not Responding

Scheduled Pinned Locked Moved Unsolved QtWebEngine
12 Posts 3 Posters 638 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.
  • Juan DevJ Offline
    Juan DevJ Offline
    Juan Dev
    wrote on last edited by Juan Dev
    #1

    Hello

    I made a simple application wich use QWebEngineView.
    This application allows you to display a very simple page.

    If I choose the "Debug" Compilation mode, everything still works correctly (with CTRL+R or Start Debugging).
    If I choose "Release" Compilation mode with CTRL+R to launch the program, occasionally the program stops responding when I start to display the page.

    Here is the application code

    main.cpp

    #include<QApplication>
    #include"MyMainWindow.h"
    
    
    int main(int nbArg, char* listeArg[])
    {
      QApplication app(nbArg,listeArg);
      MyMainWindow myWindow;
    
      myWindow.show();
    
      app.exec();
    }
    

    MyMainWindow.h

    #ifndef MYMAINWINDOW_H
    #define MYMAINWINDOW_H
    
    #include<QtWidgets>
    #include<QtWebEngineWidgets>
    
    class MyMainWindow : public QMainWindow
    {
      Q_OBJECT
    
      public:
      MyMainWindow();
    
      public slots:
      void webBrowser();
      void manageEndLoading();
    
      private:
      QAction *m_actionQuit;
      QAction *m_actionWeb;
      QMenu *m_menuFile;
      QWebEngineView *m_webViewer;
    };
    
    #endif // MYMAINWINDOW_H
    

    MyMainWindow.cpp

    #include"MyMainWindow.h"
    
    //--------------//
    // Constructor  //
    //--------------//
    MyMainWindow::MyMainWindow()
    {
      // Window's Parameters
      setWindowTitle("My Application");
      setMinimumHeight(500);
      setMinimumWidth(500);
      setWindowIcon(QIcon("./icones/application.png"));
    
      // Actions
      m_actionQuit  = new QAction(QIcon("./icones/quit.png"),"Quit",this);
      m_actionWeb   = new QAction(QIcon("./icones/web.png"),"Web Browser",this);
    
      // Menu
      m_menuFile = menuBar()->addMenu("File");
      m_menuFile->addAction(m_actionWeb);
      m_menuFile->addAction(m_actionQuit);
    
      // Creation Objects
      m_webViewer = new QWebEngineView();
    
      // Connexions
      connect(m_actionQuit,&QAction::triggered,this,&QGuiApplication::quit);
      connect(m_actionWeb,&QAction::triggered,this,&MyMainWindow::webBrowser);
    
      // Connexions
      connect(m_webViewer,&QWebEngineView::loadFinished,this,&MyMainWindow::manageEndLoading);
    }
    
    
    //------------------------//
    // Function webBrowser()  //
    //------------------------//
    void MyMainWindow::webBrowser()
    {
      // Initialisation
      QString urlPage   = "http://factice_url.com/simple_page.html";
    
      // Setting and Loading URL
      QWebEngineHttpRequest httpObjet;
      httpObjet.setUrl(urlPage);
      m_webViewer->load(httpObjet);
    
      // Set Central Frame
      setCentralWidget(m_webViewer);
    }
    
    
    //------------------------------//
    // Fonction manageEndLoading()  //
    //------------------------------//
    void MyMainWindow::manageEndLoading()
    {
      qDebug() << "Page Loaded is " << m_webViewer->page()->url().fileName();
    }
    

    I know this code may seem a bit long to explain a simple problem but I prefer to provide all the code so I know if I made a mistake.
    Because I don't understand why from time to time the program stops responding.
    Knowing that if the first click on opening the "Web Browser" does not pose a problem then all the following clicks work (at least I think so).
    On the other hand, after starting the application, at the first click the program no longer responds.

    And I have the impression that this only happens in Release Mode with CTRL+R
    Hoping to be clear :)

    M 1 Reply Last reply
    0
    • Juan DevJ Juan Dev

      Hello

      I made a simple application wich use QWebEngineView.
      This application allows you to display a very simple page.

      If I choose the "Debug" Compilation mode, everything still works correctly (with CTRL+R or Start Debugging).
      If I choose "Release" Compilation mode with CTRL+R to launch the program, occasionally the program stops responding when I start to display the page.

      Here is the application code

      main.cpp

      #include<QApplication>
      #include"MyMainWindow.h"
      
      
      int main(int nbArg, char* listeArg[])
      {
        QApplication app(nbArg,listeArg);
        MyMainWindow myWindow;
      
        myWindow.show();
      
        app.exec();
      }
      

      MyMainWindow.h

      #ifndef MYMAINWINDOW_H
      #define MYMAINWINDOW_H
      
      #include<QtWidgets>
      #include<QtWebEngineWidgets>
      
      class MyMainWindow : public QMainWindow
      {
        Q_OBJECT
      
        public:
        MyMainWindow();
      
        public slots:
        void webBrowser();
        void manageEndLoading();
      
        private:
        QAction *m_actionQuit;
        QAction *m_actionWeb;
        QMenu *m_menuFile;
        QWebEngineView *m_webViewer;
      };
      
      #endif // MYMAINWINDOW_H
      

      MyMainWindow.cpp

      #include"MyMainWindow.h"
      
      //--------------//
      // Constructor  //
      //--------------//
      MyMainWindow::MyMainWindow()
      {
        // Window's Parameters
        setWindowTitle("My Application");
        setMinimumHeight(500);
        setMinimumWidth(500);
        setWindowIcon(QIcon("./icones/application.png"));
      
        // Actions
        m_actionQuit  = new QAction(QIcon("./icones/quit.png"),"Quit",this);
        m_actionWeb   = new QAction(QIcon("./icones/web.png"),"Web Browser",this);
      
        // Menu
        m_menuFile = menuBar()->addMenu("File");
        m_menuFile->addAction(m_actionWeb);
        m_menuFile->addAction(m_actionQuit);
      
        // Creation Objects
        m_webViewer = new QWebEngineView();
      
        // Connexions
        connect(m_actionQuit,&QAction::triggered,this,&QGuiApplication::quit);
        connect(m_actionWeb,&QAction::triggered,this,&MyMainWindow::webBrowser);
      
        // Connexions
        connect(m_webViewer,&QWebEngineView::loadFinished,this,&MyMainWindow::manageEndLoading);
      }
      
      
      //------------------------//
      // Function webBrowser()  //
      //------------------------//
      void MyMainWindow::webBrowser()
      {
        // Initialisation
        QString urlPage   = "http://factice_url.com/simple_page.html";
      
        // Setting and Loading URL
        QWebEngineHttpRequest httpObjet;
        httpObjet.setUrl(urlPage);
        m_webViewer->load(httpObjet);
      
        // Set Central Frame
        setCentralWidget(m_webViewer);
      }
      
      
      //------------------------------//
      // Fonction manageEndLoading()  //
      //------------------------------//
      void MyMainWindow::manageEndLoading()
      {
        qDebug() << "Page Loaded is " << m_webViewer->page()->url().fileName();
      }
      

      I know this code may seem a bit long to explain a simple problem but I prefer to provide all the code so I know if I made a mistake.
      Because I don't understand why from time to time the program stops responding.
      Knowing that if the first click on opening the "Web Browser" does not pose a problem then all the following clicks work (at least I think so).
      On the other hand, after starting the application, at the first click the program no longer responds.

      And I have the impression that this only happens in Release Mode with CTRL+R
      Hoping to be clear :)

      M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      Hi @Juan-Dev

        // Set Central Frame
        setCentralWidget(m_webViewer);
      

      Put this in the constructor.

      and why not simply use:
      QWebEngineView::load(const QUrl &url)

      Maybe trash the build folder worth a try.

      Juan DevJ 1 Reply Last reply
      0
      • M mpergand

        Hi @Juan-Dev

          // Set Central Frame
          setCentralWidget(m_webViewer);
        

        Put this in the constructor.

        and why not simply use:
        QWebEngineView::load(const QUrl &url)

        Maybe trash the build folder worth a try.

        Juan DevJ Offline
        Juan DevJ Offline
        Juan Dev
        wrote on last edited by
        #3

        @mpergand Thank you for your reply.

        First
        I use "httpObjet" to set url because later I will have to set up "header", "method" and "postData" in this object.

        Next
        The solution works fine by placing the "setCentralWidget()" in the constructor. Thank you so much.
        But if in my application I have several windows in addition to those using "QWebEngineView", I will not be able to place the code "setCentralWidget()" in the constructor of my main window...?

        M 1 Reply Last reply
        0
        • Juan DevJ Juan Dev

          @mpergand Thank you for your reply.

          First
          I use "httpObjet" to set url because later I will have to set up "header", "method" and "postData" in this object.

          Next
          The solution works fine by placing the "setCentralWidget()" in the constructor. Thank you so much.
          But if in my application I have several windows in addition to those using "QWebEngineView", I will not be able to place the code "setCentralWidget()" in the constructor of my main window...?

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

          @Juan-Dev said in QWebEngineView - Application Not Responding:

          But if in my application I have several windows in addition to those using "QWebEngineView", I will not be able to place the code "setCentralWidget()" in the constructor of my main window...?

          I'm not sure to understand you here, you can have as many web view as you want.

          Juan DevJ 1 Reply Last reply
          0
          • M mpergand

            @Juan-Dev said in QWebEngineView - Application Not Responding:

            But if in my application I have several windows in addition to those using "QWebEngineView", I will not be able to place the code "setCentralWidget()" in the constructor of my main window...?

            I'm not sure to understand you here, you can have as many web view as you want.

            Juan DevJ Offline
            Juan DevJ Offline
            Juan Dev
            wrote on last edited by Juan Dev
            #5

            @mpergand
            The application given as an example is simple.
            But then I don't just want to use "WebEngineView" but I want to use Forms for example.
            My application must therefore be able to display either an "Web Browser" or a "Form" in the same window.

            But if I use "setCentralWidget" in the constructor, I won't be able to display the forms in the same main window...?

            Hoping to be clear :)

            JonBJ 1 Reply Last reply
            0
            • Juan DevJ Juan Dev

              @mpergand
              The application given as an example is simple.
              But then I don't just want to use "WebEngineView" but I want to use Forms for example.
              My application must therefore be able to display either an "Web Browser" or a "Form" in the same window.

              But if I use "setCentralWidget" in the constructor, I won't be able to display the forms in the same main window...?

              Hoping to be clear :)

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Juan-Dev
              Still don't understand, nor what it has to do with setCentralWidget().

              If you want to display multiple widgets in a QMainWindow's central widget, set it to a generic QWidget, place a layout on it, and then add as many widgets to that as you wish.

              If you want to display one of multiple widgets (e.g. a QWebEngineView or something else) use a QStackedWidget as your containing widget for the alternatives.

              Juan DevJ 1 Reply Last reply
              0
              • JonBJ JonB

                @Juan-Dev
                Still don't understand, nor what it has to do with setCentralWidget().

                If you want to display multiple widgets in a QMainWindow's central widget, set it to a generic QWidget, place a layout on it, and then add as many widgets to that as you wish.

                If you want to display one of multiple widgets (e.g. a QWebEngineView or something else) use a QStackedWidget as your containing widget for the alternatives.

                Juan DevJ Offline
                Juan DevJ Offline
                Juan Dev
                wrote on last edited by Juan Dev
                #7

                @JonB
                First of all, thank you for your answers and your time spent.

                As stated in my previous post, the example program provided is simple but the program I am working on is a little more complex.
                And in this program, in the constructor I have not defined a Central Widget.
                In fact for each function (launched via the menu):

                • I build a Widget in which I place Widgets and Layouts to build "my display"
                • I use "setCentralWidget" to place this Widget

                And in Debug mode I never had any problems switching from a function displaying a "Web Browser" then a "Form" then a "Web Browser", etc... But in Release mode, sometimes when I launch a "Web Browser" the application no longer responds.

                M JonBJ 2 Replies Last reply
                0
                • Juan DevJ Juan Dev

                  @JonB
                  First of all, thank you for your answers and your time spent.

                  As stated in my previous post, the example program provided is simple but the program I am working on is a little more complex.
                  And in this program, in the constructor I have not defined a Central Widget.
                  In fact for each function (launched via the menu):

                  • I build a Widget in which I place Widgets and Layouts to build "my display"
                  • I use "setCentralWidget" to place this Widget

                  And in Debug mode I never had any problems switching from a function displaying a "Web Browser" then a "Form" then a "Web Browser", etc... But in Release mode, sometimes when I launch a "Web Browser" the application no longer responds.

                  M Offline
                  M Offline
                  mpergand
                  wrote on last edited by
                  #8

                  @Juan-Dev
                  Look at setCentralWidget description:

                  *void QMainWindow::setCentralWidget(QWidget widget)
                  Sets the given widget to be the main window's central widget.
                  Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.

                  It means each time you call setCentralWidget, your webView is deleted.

                  1 Reply Last reply
                  0
                  • Juan DevJ Juan Dev

                    @JonB
                    First of all, thank you for your answers and your time spent.

                    As stated in my previous post, the example program provided is simple but the program I am working on is a little more complex.
                    And in this program, in the constructor I have not defined a Central Widget.
                    In fact for each function (launched via the menu):

                    • I build a Widget in which I place Widgets and Layouts to build "my display"
                    • I use "setCentralWidget" to place this Widget

                    And in Debug mode I never had any problems switching from a function displaying a "Web Browser" then a "Form" then a "Web Browser", etc... But in Release mode, sometimes when I launch a "Web Browser" the application no longer responds.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @Juan-Dev
                    If you are falling foul of what @mpergand has written then correct your code.
                    If that is not the issue then reduce your code to a minimal, compilable, reproducible example and paste.
                    Differences between Debug and Release behaviour is usually an indication of faulty code. Erroneous code may show up in one but not the other.

                    M 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Juan-Dev
                      If you are falling foul of what @mpergand has written then correct your code.
                      If that is not the issue then reduce your code to a minimal, compilable, reproducible example and paste.
                      Differences between Debug and Release behaviour is usually an indication of faulty code. Erroneous code may show up in one but not the other.

                      M Offline
                      M Offline
                      mpergand
                      wrote on last edited by mpergand
                      #10

                      @JonB said in QWebEngineView - Application Not Responding:

                      If you are falling foul of what @mpergand has written then correct your code.

                      To be more precise:

                      If the new centralWidget is different from the old one, the old one is deleted.
                      If the two widgets are the same, nothing appends.

                      This principle applies each time you see "takes owner" in the method description.

                      Juan DevJ 1 Reply Last reply
                      1
                      • M mpergand

                        @JonB said in QWebEngineView - Application Not Responding:

                        If you are falling foul of what @mpergand has written then correct your code.

                        To be more precise:

                        If the new centralWidget is different from the old one, the old one is deleted.
                        If the two widgets are the same, nothing appends.

                        This principle applies each time you see "takes owner" in the method description.

                        Juan DevJ Offline
                        Juan DevJ Offline
                        Juan Dev
                        wrote on last edited by Juan Dev
                        #11

                        @mpergand @JonB

                        The correction proposed by @mpergand works perfectly well for the code I published.

                        But as you recommended @JonB I will publish the code of my initial application (code reduced to a minimal, compilable and reproducible example). With this code, the application still works in Debug mode but in Release mode when launching the internet browser, sometimes the application no longer responds.

                        web_browser.pro

                        QT += widgets
                        QT += webenginewidgets
                        
                        SOURCES += \
                          MyMainWindow.cpp \
                          main.cpp
                        
                        HEADERS += \
                          MyMainWindow.h
                        

                        main.cpp

                        #include<QApplication>
                        #include"MyMainWindow.h"
                        
                        int main(int nbArg, char* listeArg[])
                        {
                          QApplication app(nbArg,listeArg);
                          MyMainWindow myWindow;
                        
                          myWindow.show();
                        
                          app.exec();
                        }
                        

                        MyMainWindow.h

                        #ifndef MYMAINWINDOW_H
                        #define MYMAINWINDOW_H
                        
                        #include<QtWidgets>
                        #include<QtWebEngineWidgets>
                        
                        class MyMainWindow : public QMainWindow
                        {
                          Q_OBJECT
                        
                          public:
                          MyMainWindow();
                        
                          public slots:
                          void webBrowser();
                          void manageEndLoading();
                          void getConfig();
                        
                          private:
                          QAction *m_actionQuit;
                          QAction *m_actionWeb;
                          QAction *m_actionGetConfig;
                          QMenu *m_menuFile;
                          QWebEngineView *m_webViewer;
                        };
                        
                        #endif // MYMAINWINDOW_H
                        

                        MyMainWindow.cpp

                        #include"MyMainWindow.h"
                        
                        MyMainWindow::MyMainWindow()
                        {
                          // Window's Parameters
                          setWindowTitle("My Application");
                          setMinimumHeight(500);
                          setMinimumWidth(500);
                          setWindowIcon(QIcon("./icones/appli.png"));
                        
                          // Actions
                          m_actionQuit      = new QAction(QIcon("./icones/quit.png"),"&Quit",this);
                          m_actionWeb       = new QAction(QIcon("./icones/web.png"),"&Web Browser",this);
                          m_actionGetConfig = new QAction(QIcon("./icones/config.png"),"&Configuration",this);
                        
                          // Menu
                          m_menuFile = menuBar()->addMenu("&File");
                          m_menuFile->addAction(m_actionWeb);
                          m_menuFile->addAction(m_actionGetConfig);
                          m_menuFile->addAction(m_actionQuit);
                        
                          // Connexions
                          connect(m_actionQuit,&QAction::triggered,this,&QGuiApplication::quit);
                          connect(m_actionWeb,&QAction::triggered,this,&MyMainWindow::webBrowser);
                          connect(m_actionGetConfig,&QAction::triggered,this,&MyMainWindow::getConfig);
                        }
                        
                        
                        void MyMainWindow::webBrowser()
                        {
                          // Creation Objects
                          m_webViewer = new QWebEngineView();
                        
                          // Initialisation
                          QString urlPage   = "http://factice_url/simple_page.html";
                        
                          // Setting and Loading URL
                          QWebEngineHttpRequest httpObjet;
                          httpObjet.setUrl(urlPage);
                          m_webViewer->load(httpObjet);
                        
                          // Set Central Frame
                          setCentralWidget(m_webViewer);
                        
                          // Connexions
                          connect(m_webViewer,&QWebEngineView::loadFinished,this,&MyMainWindow::manageEndLoading);
                        }
                        
                        
                        void MyMainWindow::getConfig()
                        {
                          // Here could be read a file containing the configuration
                        
                          QGridLayout *calqueFenetre = new QGridLayout(); calqueFenetre->setAlignment(Qt::AlignTop);
                          QWidget *widgetCentre = new QWidget();
                        
                          calqueFenetre->addWidget(new QLabel("<b>Configuration</b>"));
                          calqueFenetre->addWidget(new QLabel("Here could be displayed information from reading a file for example"));
                        
                          widgetCentre->setLayout(calqueFenetre);
                          setCentralWidget(widgetCentre);
                        }
                        
                        
                        void MyMainWindow::manageEndLoading()
                        {
                          qDebug() << "Page Loaded ";
                          qDebug() << m_webViewer->page()->url().fileName();
                        }
                        

                        With the code above I had no errors in Debug Mode but sometimes in Release Mode.

                        So I decided to deport the creation of the "m_webViewer" object and its connection to my constructor.
                        Then I presented part of my code and that's where @mpergand advised me to also deport the "setCentralWidget(m_webViewer)" code into the constructor.

                        But if I export the setCentralWidget(m_webViewer) in the constructor and launch the "Configuration" menu then the "Web" menu then the application crashes.

                        1 Reply Last reply
                        0
                        • Juan DevJ Offline
                          Juan DevJ Offline
                          Juan Dev
                          wrote on last edited by
                          #12

                          If I keep the code posted above and if I modify my webBrowser function with this code

                          void MyMainWindow::webBrowser()
                          {
                            // Creation Objects
                            m_webViewer = new QWebEngineView();
                          
                            // Initialisation
                            QString urlPage   = "http://factice_url/simple_page.html";
                          
                            // Setting and Loading URL (HTTP Object used because subsequently need to apply headers)
                            QWebEngineHttpRequest httpObjet;
                            httpObjet.setUrl(urlPage);
                            m_webViewer->load(httpObjet);
                          
                            // Connexions
                            connect(m_webViewer,&QWebEngineView::loadStarted,this,&MyMainWindow::manageBeginLoading);
                            connect(m_webViewer,&QWebEngineView::loadFinished,this,&MyMainWindow::manageEndLoading);
                          }
                          

                          And if I add this function

                          void MyMainWindow::manageBeginLoading()
                          {
                            // Set Central Frame
                            setCentralWidget(m_webViewer);
                          }
                          

                          Application works in mode Debug and in mode Release without problem.
                          With this changes I move setCentralWidget(m_webViewer) into the function launched when signal loadStarted is emitted.

                          But is this correct or should we deport the creation of the m_webViewer and the connections in the constructor..?

                          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