QWebEngineView - Application Not Responding
-
@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...? -
@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.
-
@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 :)
-
@Juan-Dev
Still don't understand, nor what it has to do withsetCentralWidget()
.If you want to display multiple widgets in a
QMainWindow
's central widget, set it to a genericQWidget
, 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 aQStackedWidget
as your containing widget for the alternatives. -
@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.
-
@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.
-
@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. -
@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.
-
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. -
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 movesetCentralWidget(m_webViewer)
into the function launched when signalloadStarted
is emitted.But is this correct or should we deport the creation of the
m_webViewer
and the connections in the constructor..?