“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Introduce a,

QWebEngineView is a new browser engine added in QT5.4 for editing and viewing Web content.

In Windows, QWebEngineView supports MSVC compiler compilation but not MINGW compilation.

To use QWebEngineView, add a reference to the WebEngineWidgets module in the project file and add the #include

header.

Header:
#include <QWebEngineView> 
qmake:
QT += webenginewidgets
Since:
Qt 5.4
Inherits:
QWidget
Copy the code

Here is the official translation:

The QWebEngineView class provides a widget for viewing and editing Web documents. The Web View is the Qt WebEngine, which is the main widget component of the Web browsing module. It can be used in a variety of applications to display Web content from the Internet in real time.

QWebEngineView can load a website into a Web view using the load() function, the GET method is always used to load the URL, and as with all Qt widgets, the show() function must be called to display the Web view, or the website can be loaded using setUrl(). If the HTML file is available locally, you can use setHtml() instead.

The loadStarted() signal is emitted when the view starts loading, and the loadProgress() signal is emitted when an element of the Web view, such as an embedded image or script, finishes loading. When the view is fully loaded, the loadFinished() signal is emitted. Its arguments (true or false) indicate whether the load succeeded or failed. The QWebEngineView contains a QWebEnginePage, which in turn allows access to the QWebEngineHistory in the page context.

You can access the title of an HTML document using the title() attribute. In addition, websites can specify an icon that can be accessed using the icon() or iconUrl() property. If the title or icon changes, the corresponding titleChanged(), iconChanged(), and iconUrlChanged() signals are emitted. The zoomFactor() property allows web content to be scaled by a scale factor.

The widget has a context menu that can be customized based on the element at hand and includes actions that are useful in the browser. For custom context menus, or for embedded actions in menus or toolbars, single actions can be used through pageAction(). Web view maintenance returns the state of the action, but allows modification of action properties, such as text or ICONS. Action semantics can also be triggered directly by triggerPageAction(). If you want to support web sites that allow users to open new Windows, such as pop-ups, you can subclass QWebEngineView and re-implement the createWindow() function.

Example code for simple use of QWebEngineView:

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

QWebEngineView: QWebEngineView

Common functions:1.Find text on a web pagevoid  findText(constQString &subString, QWebEnginePage::FindFlags options = ... .const QWebEngineCallback<bool> &resultCallback = ...)Returns the currently selected text QStringselectedText(a) const2. This property saves whether the page contains the selected content.bool hasSelection(a) const3. Returns a pointer to the view history of the navigated page. QWebEngineHistory *history(a) const4. Return to the current web page icon QIconicon(a) const5. Return to the QUrl of the current web page iconiconUrl(a) const6. Load a new web addressvoid load(const QUrl &url)7. Load a new requestvoid load(const QWebEngineHttpRequest &request)8. Returns a pointer to the current page. QWebEnginePage *page(a) const9. Returns a pointer to the QAction that encapsulates the specified Web action. QAction *pageAction(QWebEnginePage::WebAction action) const10. Set the content of the Web view to datavoid setContent(const QByteArray &data, const QString &mimeType = QString(), const QUrl &baseUrl = QUrl())11. Set the HTML contentvoid setHtml(const QString &html, const QUrl &baseUrl = QUrl())12. Setup page: Make the web page a new page in the Web view. void setPage(QWebEnginePage *page)13.Void setUrl(const qur& url) QUrl () const14.Void setZoomFactor(qreal factor) qreal zoomFactor() const15.Returns a pointer to a view or page-specific setting object. QWebEngineSettings *settings() const16.Current page title QString title() const17.Triggers the specified operation. void triggerPageAction(QWebEnginePage::WebAction action, bool checked = false) convenient slot function:void back(a)Return to previous page - no responsevoid forward(a)Return to next page - No responsevoid reload(a)Reload current page - Refresh pagevoid stop(a)Stop page loading can be associated with the signal:1.Void iconUrlChanged(const QIcon &icon) void iconUrlChanged(const QUrl &url)2.Void loadFinished(bool OK)3.Loading schedule0~100
void loadProgress(int progress)
4.This signal is emitted when a new load of the page begins. void loadStarted()5.This signal is emitted when the rendering process terminates with a non-zero exit state. TerminationStatus is the terminationStatus of the process and exitCode is the status code used when the process terminates. void renderProcessTerminated(QWebEnginePage::RenderProcessTerminationStatus terminationStatus, int exitCode)6.This signal is emitted whenever the selection changes. Note: When selecting text by left-clicking and dragging with the mouse, each selected new character is signaled instead of releasing the left mouse button. void selectionChanged()7.Void titleChanged(const QString &title)8.Void urlChanged(const QUrl &url)Copy the code

Second, design procedures, complete web browsing

Current environment: windows10 64 system, QT5.12.6 + VS2017

Operation effect:

2.1 New Construction project

(1) Select the VS compiler when creating the project.

(2) Add the WebEngineWidgets module to the Pro project file.

QT += webenginewidgets
Copy the code

2.2 UI Design

2.3 the widget. The CPP

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    / / new a QWebEngineView
    m_webView = new QWebEngineView(this);

    // Add to the layout
    ui->verticalLayout_web->addWidget(m_webView);

    // Correlate signals
    connect(m_webView, SIGNAL(iconChanged(const QIcon &)), this.SLOT(slot_iconChanged(const QIcon &)));
    connect(m_webView, SIGNAL(loadProgress(int)), this.SLOT(slot_loadProgress(int)));
    connect(m_webView, SIGNAL(titleChanged(const QString &)), this.SLOT(slot_titleChanged(const QString &)));

    ui->progressBar->setMaximum(100);
    ui->progressBar->setMinimum(0);
}

Widget::~Widget()
{
    delete ui;
}

// Refresh the page
void Widget::on_pushButton_new_clicked(a)
{
     ui->progressBar->setValue(0);
     m_webView->reload(a); }// Load a new page
void Widget::on_pushButton_load_clicked(a)
{
    QString url=ui->lineEdit->text(a);if(! url.isEmpty())
    {
        ui->progressBar->setValue(0);
        m_webView->load(QUrl(url)); }}/ / stop
void Widget::on_pushButton_stop_clicked(a)
{
     m_webView->stop(a); }/ / back
void Widget::on_pushButton_up_clicked(a)
{
    m_webView->back(a); }/ / the next page
void Widget::on_pushButton_dn_clicked(a)
{
    m_webView->forward();
}

// The icon changes
void Widget::slot_iconChanged(const QIcon &icon)
{
    this->setWindowIcon(icon);
}
// Load progress
void Widget::slot_loadProgress(int progress)
{
    ui->progressBar->setValue(progress);
}
// Title change
void Widget::slot_titleChanged(const QString &title)
{
    this->setWindowTitle(title);
}
Copy the code

2.4 widget. H

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QWebEngineView>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget(a);private slots:
    void slot_iconChanged(const QIcon &icon);
    void slot_loadProgress(int progress);
    void slot_titleChanged(const QString &title);

    void on_pushButton_new_clicked(a);

    void on_pushButton_load_clicked(a);

    void on_pushButton_stop_clicked(a);

    void on_pushButton_up_clicked(a);

    void on_pushButton_dn_clicked(a);

private:
    Ui::Widget *ui;
    QWebEngineView *m_webView;
};
#endif // WIDGET_H

Copy the code

2.5 XXX. Pro

QT       += core gui
QT       += webenginewidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    Disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    widget.cpp

HEADERS += \
    widget.h

FORMS += \
    widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:! android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target

Copy the code