一,背景
混合开发已经成为了主流,利用Qt+ cef ,界面部分用h5 逻辑用c++,未尝不是一种好办法。QML自带的QtWebEngine 是一种思路。Qt+cef 也是一种思路,并且已经有大神针对Qt将cef进行了封装,那就是QCefView。
二,编译
(1),源码下载
https://github.com/tishion/QCefView
(2),cmake 编译
win32编译失败,x64可以
(3),demo 演示
程序自带了 一个例子,QCefViewTest
加载完html文件后,剩下就是 浏览器与Qt的交互了。一共两种模式,浏览器给Qt发消息和Qt给浏览器发消息
(4),自己写个demo
c++
#include
#include
#include
#include "customcefview.h"
#pragma comment (lib, "User32.lib")
CustomCefView::CustomCefView(const QString& url, QWidget* parent)
: QCefView(url, parent)
{}
CustomCefView::~CustomCefView() {
::PostMessage((HWND)winId(), WM_CLOSE, 0, 0);
}
void CustomCefView::toast(const QString &msg)
{
QCefEvent event("toastChangedEvent");
event.setStringProperty("toast", msg);
broadcastEvent("toastChange", event);
}
void
CustomCefView::onInvokeMethodNotify(int browserId, int frameId, const QString& method, const QVariantList& arguments)
{
if (0 == method.compare("login")) {
if(arguments.size()==2){
QString account = arguments.at(0).toString();
QString pwd = arguments.at(1).toString();
if(account=="1"&&pwd=="2"){
emit loginSuccess();
}else{
toast(QStringLiteral("用户名或密码错误"));
}
}
}
}
h5
https://download.csdn.net/download/weixin_38416696/13644985
保存登录信息配置:
QCefSetting::setCachePath(qApp->applicationDirPath());
QCefSetting::setPersistSessionCookies(true);
QCefSetting::setPersistUserPreferences(true);
QCefSetting::setUserDataPath(qApp->applicationDirPath());