Qt's QWebEngineVie framework and recording web page

QWebEngine architecture based on Widget

Through this article, you can roughly understand several key classes of QWebEngine framework based on Widget.


As can be seen from the above figure, it presents a tree structure from top to bottom.

QWebEngineView

A web view is the main widget component of the Qt WebEngine web browsing module. It can be used in various applications to display web content live from the Internet.

According to Qt's official description, QWebEngineView has the basic ability to load web pages from the Internet.
As can be seen from the official Demo

QWebEngineView *view = new QWebEngineView(parent);
view->load(QUrl("http://qt-project.org/"));
view->show();

Then, what interfaces are included in the specific class

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWebEngine module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QWEBENGINEVIEW_H
#define QWEBENGINEVIEW_H

#include <QtGui/qpainter.h>
#include <QtNetwork/qnetworkaccessmanager.h>
#include <QtWidgets/qwidget.h>

#include <QtWebEngineWidgets/qtwebenginewidgetsglobal.h>
#include <QtWebEngineWidgets/qwebenginepage.h>
#include <QtWebEngineCore/qwebenginehttprequest.h>

QT_BEGIN_NAMESPACE
class QContextMenuEvent;
class QUrl;
class QWebEnginePage;
class QWebEngineSettings;
class QWebEngineViewAccessible;
class QWebEngineViewPrivate;

class QWEBENGINEWIDGETS_EXPORT QWebEngineView : public QWidget {
    Q_OBJECT
    Q_PROPERTY(QString title READ title)
    Q_PROPERTY(QUrl url READ url WRITE setUrl)
    Q_PROPERTY(QUrl iconUrl READ iconUrl NOTIFY iconUrlChanged)
    Q_PROPERTY(QIcon icon READ icon NOTIFY iconChanged)
    Q_PROPERTY(QString selectedText READ selectedText)
    Q_PROPERTY(bool hasSelection READ hasSelection)
    Q_PROPERTY(qreal zoomFactor READ zoomFactor WRITE setZoomFactor)

public:
    explicit QWebEngineView(QWidget* parent = Q_NULLPTR);
    virtual ~QWebEngineView();

    QWebEnginePage* page() const;            //QWebEnginePage
    void setPage(QWebEnginePage* page);

    void load(const QUrl &url);
    void load(const QWebEngineHttpRequest &request);
    void setHtml(const QString& html, const QUrl& baseUrl = QUrl());
    void setContent(const QByteArray& data, const QString& mimeType = QString(), const QUrl& baseUrl = QUrl());

    QWebEngineHistory* history() const;

    QString title() const;
    void setUrl(const QUrl &url);
    QUrl url() const;
    QUrl iconUrl() const;
    QIcon icon() const;

    bool hasSelection() const;
    QString selectedText() const;

#ifndef QT_NO_ACTION
    QAction* pageAction(QWebEnginePage::WebAction action) const;
#endif
    void triggerPageAction(QWebEnginePage::WebAction action, bool checked = false);

    qreal zoomFactor() const;
    void setZoomFactor(qreal factor);
    void findText(const QString &subString, QWebEnginePage::FindFlags options = QWebEnginePage::FindFlags(), const QWebEngineCallback<bool> &resultCallback = QWebEngineCallback<bool>());

    QSize sizeHint() const override;
    QWebEngineSettings *settings() const;

public Q_SLOTS:
    void stop();
    void back();
    void forward();
    void reload();

Q_SIGNALS:
    void loadStarted();
    void loadProgress(int progress);
    void loadFinished(bool);
    void titleChanged(const QString& title);
    void selectionChanged();
    void urlChanged(const QUrl&);
    void iconUrlChanged(const QUrl&);
    void iconChanged(const QIcon&);
    void renderProcessTerminated(QWebEnginePage::RenderProcessTerminationStatus terminationStatus,
                             int exitCode);

protected:
    virtual QWebEngineView *createWindow(QWebEnginePage::WebWindowType type);
#if QT_CONFIG(contextmenu)
    void contextMenuEvent(QContextMenuEvent*) override;
#endif // QT_CONFIG(contextmenu)
    bool event(QEvent*) override;
    void showEvent(QShowEvent *) override;
    void hideEvent(QHideEvent *) override;
    void closeEvent(QCloseEvent *) override;
#if QT_CONFIG(draganddrop)
    void dragEnterEvent(QDragEnterEvent *e) override;
    void dragLeaveEvent(QDragLeaveEvent *e) override;
    void dragMoveEvent(QDragMoveEvent *e) override;
    void dropEvent(QDropEvent *e) override;
#endif // QT_CONFIG(draganddrop)

private:
    Q_DISABLE_COPY(QWebEngineView)
    Q_DECLARE_PRIVATE(QWebEngineView)
    QScopedPointer<QWebEngineViewPrivate> d_ptr;

    friend class QWebEnginePage;
    friend class QWebEnginePagePrivate;
#if QT_CONFIG(accessibility)
    friend class QWebEngineViewAccessible;
#endif
};

QT_END_NAMESPACE

#endif // QWEBENGINEVIEW_H

Through the above class definition, we can find that there is a page() interface. The description of the page() interface is as follows:

The page() function returns a pointer to a web page object. A QWebEngineView contains a QWebEnginePage, which in turn allows access to the QWebEngineHistory in the page's context.

  • The page() function returns a pointer to the web page object. QWebEngineView contains a QWebEnginePage, which in turn allows access to QWebEngineHistory in the context of the page.

Questions about QWebEnginePage under QWebEngineView

As mentioned above, page() returns a pointer to a web page object. QWebEngineView contains a QWebEnginePage. Can it contain more than one? As you know, our Chrome browser has multiple tabs when opening multiple web pages. Can the "tabs" here correspond to the pages here?

This brings me to another interface: setpage (qwebenginepage)*

void QWebEngineView::setPage(QWebEnginePage *page)

Makes page the new web page of the web view.
The parent QObject of the provided page remains the owner of the object. If the current page is a child of the web view, it will be deleted.

  • The parent QObject of the provided page is still the owner of the object. If the current page is a sub page of the web view, it will be deleted.

Seeing here, I'm a little confused. After a new page is set, the current page will be deleted? Isn't there always only one page, which can't have the effect of multiple tabs.

It should be my wrong understanding. Yes, I saw the official Demo. I'll explain it in detail later.

QWebEnginePage

A web engine page holds the contents of an HTML document, the history of navigated links, and actions.

As described in the above structure diagram, the qWeb engine page contains HTML content, navigation history, links, and some Actions.
These Actions include: Forward, back, stop, Reload, etc.

  • Three contents under QWebEnginePage

QWebEngineHistory

Each web engine page contains a history of visited pages that can be accessed by QWebEnginePage::history().

Blind guessing is something similar to historical records. Let's not learn more.

QWebEngineAction

QWebEngineProfile

A web engine profile contains settings, scripts, persistent cookie policy, and the list of visited links shared by all web engine pages that belong to the profile.

See what QWebEngineProfile contains

  • settings
  • scripts
  • cookie

QWebEngineSettings

QWebEngineSettings allows configuration of browser properties, such as font sizes and families, the location of a custom style sheet, and generic attributes, such as JavaScript support. Individual attributes are set using the setAttribute() function. The WebAttribute enum further describes each attribute.

You can guess one, two or three by looking at the type name, like the settings of a browser.

  • Allows you to set browser properties, such as font size and type, and some common properties, such as whether JavaScript is supported. WebAttribute describes many related attributes.

QWebEngineScript

QWebEngineScript enables the programmatic injection of so called user scripts in the JavaScript engine at different points, determined by injectionPoint(), during the loading of web contents.

QWebEngineScript allows so-called user scripts to be programmatically injected at different points in the JavaScript engine (determined by injectionPoint()) during Web content loading

Cookie related

There are probably several interfaces related to cookies, which are recorded here and will not be studied for the time being

QWebEngineCookieStore *QWebEngineProfile::cookieStore();
QWebEngineProfile::PersistentCookiesPolicy	persistentCookiesPolicy() const;
void	setPersistentCookiesPolicy(QWebEngineProfile::PersistentCookiesPolicy newPersistentCookiesPolicy);

QWebEnginePage load web page

Under the QWebEngineView class, there is also a QWebEnginePage class. Although QWebEngineView also has a loadUrl interface, most of the time, the web page is loaded with QWebEngineView page. Because there is an interface in QWebEnginePage that can run javascript, and the browsing history and navigation of web pages can be provided through QWebEngineHistory, as well as some web pages operated by QAction.

Two forms of loading interface

In QWebEnginePage, there are actually two interfaces

  • load() interface
void QWebEnginePage::load(const QUrl &url)
// Loads url into this page.
// Note: The view remains the same until enough data has arrived to display the new URL.
// See also setUrl(), setHtml(), and setContent().

Load the URL directly into the page, and the new URL content will be displayed only when there is enough data. The interface is relatively simple.

  • setHtml() interface
void QWebEnginePage::setHtml(const QString &html, const QUrl &baseUrl = QUrl())
// Sets the content of this page to html. baseUrl is optional and used to 
//resolve relative URLs in the document, such as referenced images or stylesheets.

The first parameter of the interface can be understood as the content of an html file, such as a blank file:

#define BLANK_HTML  "<!DOCTYPE html><html><head></head><body></body></html>"

The html is loaded immediately, external objects are loaded asynchronously.

Unlike the load interface, html content is loaded immediately and external resources are loaded asynchronously. In other words, there is no need to wait for enough data to be loaded like the load() interface before displaying new content.

If a script in the html runs longer than the default script timeout (currently 10 seconds), for example due to being blocked by a modal JavaScript alert dialog, this method will return as soon as possible after the timeout and any subsequent html will be loaded asynchronously

If the running time of the script in the html file exceeds the default script timeout of 10s, such as blocking timeout due to an alert dialog box, the method will return immediately and load any subsequent html asynchronously.

Three signals in the process of loading web pages

The process of web page loading is divided into three steps:

  • start
  • Loading
  • end
Q_SIGNALS:
    void loadStarted();             
    void loadProgress(int progress);
    void loadFinished(bool);

When you want to know the length of time to load a web page, you can type the time at loadStarted() and loadFinished(). The loadProgress() signal is continuously transmitted, such as 40% and 80%.

Actions after loading web pages

Some operations may explicitly need to be performed after the web page is opened, so subsequent operations can be performed after receiving the loadFinished() signal. For example, open the camera and start a new thread. Otherwise, some unexpected actions may have been performed without being ready. Timing can be controlled.

WebEngine hot start

During initialization, if a blank page is not loaded through webEngine, when you click the business for the first time and load other pages, it will cause the problem of very slow opening of web pages. Therefore, during initialization, the time to enter specific services is shortened by loading a blank page.

Keywords: Qt

Added by hairyjim on Sat, 11 Dec 2021 10:26:44 +0200