下载上图箭头所指链接,解压后按下图重命名文件夹,并新建 build 和 sdk 两个文件夹

用 CMake 工具编译上述 CEF 包,按下图配置
编译成后点击上图中的 Open Project 进入 VS2019 中



Widget 提升为 QWebEngineView

.pro 中添加头文件
QT += core gui webenginewidgets
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {
ui->setupUi(this);
ui->web_widget->setUrl(QUrl("http://www.baidu.com"));
ui->web_widget->show();
}
Widget::~Widget() {
delete ui;
}


#pragma once
#include
#include "ui_QtWebDemo.h"
#include "WebObject.h"
class QtWebDemo : public QWidget {
Q_OBJECT
public:
QtWebDemo(QWidget *parent = Q_NULLPTR);
~QtWebDemo();
public slots:
void on_btnSend_clicked();
void update_text(const QString& htmltext);
private:
Ui::QtWebDemoClass ui;
WebObject* m_pWebObj = nullptr;
};
#include "QtWebDemo.h"
#include
#include
QtWebDemo::QtWebDemo(QWidget *parent) : QWidget(parent) {
ui.setupUi(this);
QString path = QApplication::applicationDirPath() + "/WebPage/index.html";
ui.webEngineView->setUrl(QUrl(path));
m_pWebObj = new WebObject();
QWebEnginePage* pPage = ui.webEngineView->page();
// Qt 和 html js 的桥梁:QWebChannel
QWebChannel* channel = new QWebChannel(this);
// 注册 html 对象
// 第二个参数需要是 QObject 的派生类
channel->registerObject(QStringLiteral("content"), m_pWebObj);
pPage->setWebChannel(channel);
connect(m_pWebObj, &WebObject::sig_SendToUI, this, &QtWebDemo::update_text);
}
void QtWebDemo::on_btnSend_clicked() {
QString text = ui.lineEdit->text();
m_pWebObj->SendTextToHtml(text);
}
QtWebDemo::~QtWebDemo() {
delete m_pWebObj;
}
void QtWebDemo::update_text(const QString& htmltext) {
ui.plainTextEdit->appendPlainText(htmltext);
}
#pragma once
#include
class WebObject : public QObject {
Q_OBJECT
public:
WebObject(QObject* parent = nullptr) :QObject(parent) {}
~WebObject();
void SendTextToHtml(const QString& text);
// 接收来自 html 发来的内容,必须要加 Q_INVOKABLE,不然收不到
Q_INVOKABLE void receiveTextFromHtml(const QString& r_text);
signals:
void sig_sendTextToHtml(const QString& text);
void sig_SendToUI(const QString& htmltext);
};
#include "WebObject.h"
WebObject::~WebObject() {}
void WebObject::SendTextToHtml(const QString& text) {
emit sig_sendTextToHtml(text);
}
void WebObject::receiveTextFromHtml(const QString& htmltext) {
emit sig_SendToUI(htmltext);
}

#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {
ui->setupUi(this);
QString exe_path = qApp->applicationDirPath();
QString _klinePath = exe_path + "/myecharts/candlestick-simple.html";
ui->web_widget->setUrl(QUrl(_klinePath));
connect(ui->radioButton_kline, &QRadioButton::toggled, [=](bool checked){
if(checked) {
ui->web_widget->setUrl(QUrl(_klinePath));
}
});
connect(ui->radioButton_line, &QRadioButton::toggled, [=](bool checked){
if(checked) {
QString _linePath = exe_path + "/myecharts/line-smooth.html";
ui->web_widget->setUrl(QUrl(_linePath));
}
});
}
Widget::~Widget() {
delete ui;
}