Why is QWebEngineView::loadFinished triggered only once?
-
Here is the code
CMakeLists.txt
cmake_minimum_required(VERSION 3.5) project(test_QWebEngineView_loading LANGUAGES CXX) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt5 COMPONENTS REQUIRED Core Widgets WebEngineWidgets) set(SOURCE_FILES main.cpp test.cpp) add_executable(test_QWebEngineView_loading ${SOURCE_FILES}) target_link_libraries(test_QWebEngineView_loading PRIVATE Qt5::Core Qt5::Widgets Qt5::WebEngineWidgets)
main.cpp
#include <QApplication> #include "test.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Test w; w.resize(800,600); w.show(); return app.exec(); }
test.h
#ifndef TEST_H #define TEST_H #include <QMainWindow> #include <QWebEngineView> class Test : public QMainWindow { Q_OBJECT public: explicit Test(QWidget* parent = nullptr); ~Test() override; public slots: private: QWebEngineView *m_website; QString m_url; }; #endif // TEST_H
test.cpp
// Qt headers #include <QUrl> #include <QTimer> // Other headers #include "test.h" Test::Test(QWidget* parent /*=nullptr*/): QMainWindow(parent) {; m_website = new QWebEngineView(); setCentralWidget(m_website); ///////////////////////////////////// QObject::connect(m_website, &QWebEngineView::loadFinished, [this](bool isOk) { if (!isOk) { qDebug() << "Error when loading URL"; return; } qDebug() << "Loading " << m_url << "is finished"; }); // one loads a first URL // Paris m_url = QString("https://www.openstreetmap.org/#map=11/48.8578/2.4093"); qDebug() << "loading " << m_url << "..."; m_website->load(QUrl(m_url)); // waiting 2 seconds and then loading a new URL QTimer::singleShot(2000, [this](){ // 2 sec. // Ha Noi 21.0102/105.8134 m_url = QString("https://www.openstreetmap.org/#map=11/21.0102/105.8134"); qDebug() << "loading " << m_url << "..."; m_website->load(QUrl(m_url)); }); } // destructor Test::~Test() { delete m_website; }
Here is the output:
loading "https://www.openstreetmap.org/#map=11/48.8578/2.4093" ...
Loading "https://www.openstreetmap.org/#map=11/48.8578/2.4093" is finished
loading "https://www.openstreetmap.org/#map=11/21.0102/105.8134" ...I am wondering why the signal &QWebEngineView::loadFinished is not emitted when I load the second URL while the page is well displayed in the QMainWindow. Why do I not see the line "Loading "https://www.openstreetmap.org/#map=11/21.0102/105.8134 is finished"?
-
Which Qt version / Qt WebEngine version are you testing with?
THere were a couple of fixes related to loadFinished() in Qt 5.15, namely https://codereview.qt-project.org/c/qt/qtwebengine/+/340965
-
I use Qt 5.15.3 from Ubuntu 22.04 packages. I guess this is the same version for QtWebEngine.
@odelaune
Not an answer to how many timesloadFinished
is called, but be careful if you use code like this for real. Yourm_url
is altered after 2 seconds, your firstloadFinished
should not assume that it is still its original url by the time the slot is called....