开发环境:Qt6.8.0+MSVC2022
.pro中增加
QT += core gui webenginewidgets
头文件
#ifndef CWIDGET_WEB_ENGINEVIEW_H
#define CWIDGET_WEB_ENGINEVIEW_H
#include <QWebEngineView>
#include <QWebEnginePage>
#include <QMouseEvent>
#include <QDateTime>
#include <QObject>
#include <QUrl>
#include <QWebEngineProfile>
#include <QWebEngineSettings>
#include <QWebChannel>
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>
class WebBridge : public QObject {
Q_OBJECT
public:
explicit WebBridge(QObject *parent = nullptr) : QObject(parent) {}
public slots:
void buttonClicked(QString str){emit sig_SendSignal_ToEngineView_ButtonID(str);}
signals:
void sig_SendSignal_ToEngineView_ButtonID(QString);
};
class CWidget_Web_EngineView : public QWebEngineView
{
Q_OBJECT
public:
CWidget_Web_EngineView(QWidget *parent = nullptr);
void goLoad(QString);
void goBack();//上一页
void goForWard();//下一页
void goReload();//重新加载
protected://点击链接-需要创建新窗口时触发
QWebEngineView* createWindow(QWebEnginePage::WebWindowType type) override;
//反馈给主界面的信号
signals:
void sig_SendSignal_ToWidget_finished(bool);
void sig_SendSignal_ToWidget_Progress(int);
void sig_SendSignal_ToWidget_ButtonID(QString);
private slots://鼠标悬停时选中的网址
void slot_LinkHovered(const QString& url);
private slots://网页加载进度
void slot_loadProgress(int progress);
void slot_LoadFinished(bool);
private:
QUrl m_Current_Url;//记录link网址
private:
WebBridge *bridge;
};
#endif // CWIDGET_WEB_ENGINEVIEW_H
源文件
#include "CWidget_Web_EngineView.h"
CWidget_Web_EngineView::CWidget_Web_EngineView(QWidget *parent){
//鼠标悬停时选中的网址
connect(page(), &QWebEnginePage::linkHovered, this, &CWidget_Web_EngineView::slot_LinkHovered);
//加载过程
connect(page(), &QWebEnginePage::loadProgress, this, &CWidget_Web_EngineView::slot_loadProgress);
connect(page(), &QWebEnginePage::loadFinished, this, &CWidget_Web_EngineView::slot_LoadFinished);
bridge = new WebBridge();
QWebChannel *channel = new QWebChannel(page());
channel->registerObject(QStringLiteral("bridge"), bridge);
page()->setWebChannel(channel);
connect(bridge,SIGNAL(sig_SendSignal_ToEngineView_ButtonID(QString)),this,SIGNAL(sig_SendSignal_ToWidget_ButtonID(QString)));
}
void CWidget_Web_EngineView::goLoad(QString url){
load(url);
}
void CWidget_Web_EngineView::goBack(){
back();
}
void CWidget_Web_EngineView::goForWard(){
forward();
}
void CWidget_Web_EngineView::goReload(){
reload();
}
QWebEngineView *CWidget_Web_EngineView::createWindow(QWebEnginePage::WebWindowType){
if(!m_Current_Url.isEmpty()){
printf("createWindow:%s\n",m_Current_Url.toString().toStdString().c_str());
load(m_Current_Url);
}update();
return nullptr;
}
void CWidget_Web_EngineView::slot_LinkHovered(const QString& url){
if(url.trimmed().isEmpty()){return;}
printf("LinkHovered Url:%s\n",url.toStdString().c_str());
m_Current_Url.setUrl(url);
}
void CWidget_Web_EngineView::slot_loadProgress(int progress){
emit sig_SendSignal_ToWidget_Progress(progress);
}
void CWidget_Web_EngineView::slot_LoadFinished(bool success) {
emit sig_SendSignal_ToWidget_finished(success);
const QString script = R"(
new QWebChannel(qt.webChannelTransport, function(channel) {
window.bridge = channel.objects.bridge;
});
)";
this->page()->runJavaScript(script);
}
网页
<!DOCTYPE html>
<html>
<head>
<title>Button Click Example</title>
<script src="qwebchannel.js"></script> <!-- 确保引用的路径正确 -->
<script>
function setupButtonClickListener() {
var buttons = document.getElementsByTagName("button");
new QWebChannel(qt.webChannelTransport, function(channel) {
window.bridge = channel.objects.bridge;
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", function() {
if (bridge) {
bridge.buttonClicked(this.id);
}
});
}
});
}
document.addEventListener("DOMContentLoaded", setupButtonClickListener, false);
</script>
</head>
<body>
<button id="button1">Button 1</button>
<button id="button2">Button 2</button>
<button id="button3">Button 3</button>
</body>
</html>
注意:qwebchannel.js 放到html同级目录;qwebchannel.js文件从qt安装好的位置找;