编译libcef_dll_wrapper
假设你已经编译出了libcef_dll_wrapper.lib(Debug和Release版本,并且对应版本的程序集类型分别是:MDd和MD):qt的运行时库是MDd类型的,因此cef3编译的时候也应该遵循这个运行时库的编译方方式;qt集成cef在windows和mac下编译_LIJIWEI0611的博客-CSDN博客_cef mac
本人编译的时32位release版本
新建一个cef_bin的文件夹:拷贝cef87(本人使用的cef版本):的include Resources Release文件夹到新建的cef_bin文件夹中
同时将编译生成的Release 版本的libcef_dll_wrapper.lib拷贝到cef_bin/release目录:
将编译生成的Release 版本的libcef_dll_wrapper.lib的路径在:ef_binary_87.1.14+ga29e9a3+chromium-87.0.4280.141_windows32\build\libcef_dll_wrapper\Release\libcef_dll_wrapper.lib
这样cef_bin就准备好了:
QWidget中集成cef
1 首先qt_creator新建一个QWidget的工程
2 将cef_bin文件夹拷贝到工程下:
3 .pro文件中引入cef
4 开始集成
代码结构:
simple_app simple_handler 即 cef工程中cefSimple中的文件
cefwidget 是QWidget,集成了cef
cef_delegate 是QWidget 和simplehandle中间的桥梁,负责将simplehandle的回调传递给QWidget
cefwidget
#include "cefwidget.h"
#include "ui_cefwidget.h"
#include "include/cef_request_context.h"
#include "simple_handler.h"
#include <QTimer>
#include <QDebug>
#include <QWindow>
#include <QString>
#include <Windows.h>
#pragma comment(lib, "User32.lib")
CefWidget::CefWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::CefWidget)
{
ui->setupUi(this);
QTimer::singleShot(0,[this](){initView();});
}
CefWidget::~CefWidget()
{
delete ui;
}
void CefWidget::initView(){
CefWindowInfo cef_wnd_info;
QString str_url = "http://www.baidu.com";
RECT win_rect;
QRect rect = this->geometry();
win_rect.left = rect.left();
win_rect.right = rect.right();
win_rect.top = rect.top();
win_rect.bottom = rect.bottom();
qDebug() << rect;
cef_wnd_info.SetAsChild((HWND)this->winId(), win_rect); //将cef界面嵌入qt界面中
CefBrowserSettings cef_browser_settings;
simple_handler_ = CefRefPtr<SimpleHandler>(new SimpleHandler());
simple_handler_->setDelegate(this);
CefBrowserHost::CreateBrowser(cef_wnd_info,
simple_handler_,
str_url.toStdString(),
cef_browser_settings,
nullptr,
CefRequestContext::GetGlobalContext());
}
void CefWidget::resizeEvent(QResizeEvent *event) {
resizeView();
}
void CefWidget::resizeView(){
if (!simple_handler_ ) {
return;
}
CefRefPtr<CefBrowser> browser = simple_handler_->GetCurrentBrowser();
if (!browser)
{
return;
}
CefRefPtr<CefBrowserHost> host = browser->GetHost();
if (!host) {
return;
}
QRect rect = this->geometry();
qreal ratio = this->windowHandle() ? this->windowHandle()->devicePixelRatio()
: 1.0;
HWND hwndCefBrowser = (HWND)(host->GetWindowHandle());
if (hwndCefBrowser && ::IsWindow(hwndCefBrowser))
::MoveWindow(hwndCefBrowser, 0, 0, rect.width() * ratio, rect.height() * ratio, FALSE);
}
void CefWidget::OnBrowserLoadEnd(const std::string& url,int identifier,int status){
qDebug()<< "CefWidget::OnBrowserLoadEnd" << QString(url.c_str()) ;
resizeView();
}
main:
#include "include/cef_app.h"
#include "cefwidget.h"
#include <QtWidgets/QApplication>
#include "simple_app.h"
/**
* 初始化QT以及CEF相关
*/
int init_qt_cef(int& argc, char** argv)
{
const HINSTANCE h_instance = static_cast<HINSTANCE>(GetModuleHandle(nullptr));
const CefMainArgs main_args(h_instance);
const CefRefPtr<SimpleApp> app(new SimpleApp); //CefApp实现,用于处理进程相关的回调。
const int exit_code = CefExecuteProcess(main_args, app.get(), nullptr);
if (exit_code >= 0)
{
return exit_code;
}
// 设置配置
CefSettings settings;
settings.multi_threaded_message_loop = true; //多线程消息循环
settings.log_severity = LOGSEVERITY_DISABLE; //日志
settings.no_sandbox = true; //沙盒
CefInitialize(main_args, settings, app, nullptr);
return -1;
}
int main(int argc, char* argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); // 解决高DPI下,界面比例问题
QApplication a(argc, argv);
const int result = init_qt_cef(argc, argv);
if (result >= 0)
{
return result;
}
CefWidget w;
w.show();
a.exec();
CefShutdown(); // 关闭CEF,释放资源
return 0;
}
效果:
小问题:在cef的OnLoadEnd回调到来之前,应该加载一个动画之类的,先不让cefview显示出来,此时的cef正在初始化,尺寸还不对;以后处理吧
运行:
将cef_bin目录下的Release Resources中的文件拷贝到qt生成的可执行文件的目录下,即可成功运行
工程: