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. Why is QWebEngineView::loadFinished triggered only once?
Forum Updated to NodeBB v4.3 + New Features

Why is QWebEngineView::loadFinished triggered only once?

Scheduled Pinned Locked Moved Unsolved QtWebEngine
4 Posts 3 Posters 637 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.
  • O Offline
    O Offline
    odelaune
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • kkoehneK Offline
      kkoehneK Offline
      kkoehne
      Moderators
      wrote on last edited by
      #2

      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

      Director R&D, The Qt Company

      1 Reply Last reply
      2
      • O Offline
        O Offline
        odelaune
        wrote on last edited by
        #3

        I use Qt 5.15.3 from Ubuntu 22.04 packages. I guess this is the same version for QtWebEngine.

        JonBJ 1 Reply Last reply
        0
        • O odelaune

          I use Qt 5.15.3 from Ubuntu 22.04 packages. I guess this is the same version for QtWebEngine.

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

          @odelaune
          Not an answer to how many times loadFinished is called, but be careful if you use code like this for real. Your m_url is altered after 2 seconds, your first loadFinished should not assume that it is still its original url by the time the slot is called....

          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