Qt Web

 

引言

最近领导想在 Qt 的交易客户端上嵌入 web 页面

有的做成 QWindow 可以独立打开关闭,有的做成 QWidget 可以添加到 QTabWidget 里

 

Benchmark

Qt 想使用 Web 的话,可以选择 QWebEngine、CEF、WebView2

领导用的是 mac 上用 parallels desktop 虚拟出来的 Windows 11,性能很差

测试下来,发现 QWebEngine 稍卡、cef 很流畅、WebView2 最流畅

https://web.basemark.com/ 跑的分

QtWebEngine 使用的 6.8.3 vs2022 x64 release

cef 用的 https://github.com/CefView/QCefView

写了一些 demo,包括 C++ 和 JS 通信的,反正最后没用上,就不提了

最终是用了微软的 WebVIew2

优点是性能和原生的 chrome 几乎没差别(-5%左右),打包变小了(减少了200M左右)

缺点是不能跨平台,只能在 windows 上用

接下来重点说下 WebView2

 

下载

runtime

https://developer.microsoft.com/zh-cn/microsoft-edge/webview2/?form=MA13LH#download

sample

https://github.com/MicrosoftEdge/WebView2Samples.git

dll

https://www.nuget.org/packages/Microsoft.Web.WebView2

https://www.nuget.org/packages/Microsoft.Windows.ImplementationLibrary

image-20250726030542047

image-20250726030647031

进入网页后点击右侧的下载按钮,来下载下面两个支持包

下载后发现是.nupkg文件,右键用 7-zip 解压

 

嵌入Qt工程

webview.h

webview.cpp

动态库库引入

WebView2Loader.dll.lib

头文件引入

WebView2.h

WebView2EnvironmentOptions.h

wil 目录

使用方式

 

参考资料

Qt 集成 https://blog.csdn.net/Baizn/article/details/145581641

Qt 集成 https://blog.csdn.net/andybegin/article/details/134120854

JS 调用 https://learn.microsoft.com/zh-cn/microsoft-edge/webview2/how-to/communicate-btwn-web-native

API https://learn.microsoft.com/zh-cn/microsoft-edge/webview2/reference/win32/icorewebview2?view=webview2-1.0.3351.48

API https://learn.microsoft.com/zh-cn/microsoft-edge/webview2/reference/win32/icorewebview2environmentoptions?view=webview2-1.0.3351.48