Cef浏览器控件嵌入到Qt (引用别人写的文章,不图利,只为让更多人知道)

上篇文章,通过CMake+VS2017成功编译了libcef_dll_wrapper.lib静态库,默认使用的是MTd的链接方式,而Qt中常使用的是动态链接(即MDd),所以需要使用MDd的方式重新编译libcef_dll_wrapper.lib。
环境:Windows10 x64 + Qt5.12.3 + VS2017 + cef_binary_3.2704.1414.g185cd6c_windows64
在这里插入图片描述

Qt Creator中新建基于Widget的应用程序QtWidgetCef,使用构建组件MSVC2017 64bit。

为了快速实现,我们将cefsimple demo中的源码直接移植到QtWidgetCef中。
首先把cef目录下的include拷贝到新项目中,再将libcef_dll_wrapper.lib(静态库)以及libcef.lib(动态库导入库)拷贝到新项目的lib目录下,然后在pro文件中配置include和lib目录并链接静态库。最后将cefsimple中的simple_app.h、simple_app.cc、simple_handler.h、simple_handler.cc、simple_handler_win.cc拷贝到QtWidgetCef工程源码目录下并在项目中添加。

复制代码
#-------------------------------------------------

Project created by QtCreator 2020-01-07T14:44:06

#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = MyqtCEF
TEMPLATE = app

The following define makes your compiler emit warnings if you use

any feature of Qt which has been marked as deprecated (the exact warnings

depend on your compiler). Please consult the documentation of the

deprecated API in order to know how to port your code away from it.

DEFINES += QT_DEPRECATED_WARNINGS

You can also make your code fail to compile if you use deprecated APIs.

In order to do so, uncomment the following line.

You can also select to disable deprecated APIs only up to a certain version of Qt.

#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES +=
main.cpp
simple_app.cc
simple_handler.cc
simple_handler_win.cc
widget.cpp

HEADERS +=
simple_app.h
simple_handler.h
widget.h

Default rules for deployment.

qnx: target.path = /tmp/T A R G E T / b i n e l s e : u n i x : ! a n d r o i d : t a r g e t . p a t h = / o p t / {TARGET}/bin else: unix:!android: target.path = /opt/TARGET/binelse:unix:!android:target.path=/opt/{TARGET}/bin
!isEmpty(target.path): INSTALLS += target

win32: LIBS += -L$$PWD/lib/ -llibcef_dll_wrapper
win32: LIBS += -L&&PWD/lib/ -llibcef

INCLUDEPATH += P W D / i n c l u d e D E P E N D P A T H + = PWD/include DEPENDPATH +=PWD/includeDEPENDPATH+=PWD/include

win32:!win32-g++: PRE_TARGETDEPS += P W D / l i b / l i b c e f d l l w r a p p e r . l i b e l s e : w i n 32 − g + + : P R E T A R G E T D E P S + = PWD/lib/libcef_dll_wrapper.lib else:win32-g++: PRE_TARGETDEPS +=PWD/lib/libcefdllwrapper.libelse:win32g++:PRETARGETDEPS+=PWD/lib/liblibcef_dll_wrapper.a

LIBS += $$PWD/lib/libcef_dll_wrapper.lib
shell32.lib
kernel32.lib
user32.lib
ole32.lib
oleaut32.lib
gdi32.lib
复制代码
将代码中的#include "cefsimple/simple_handler.h"替换为#include “simple_handler.h”,因为路径改变了~ 打开main.cpp,将主函数改为如下代码:

复制代码
1 int main(int argc, char* argv[]) {
2 // Enable High DPI support.
3 CefEnableHighDPISupport();
4
5 // Structure for passing command-line arguments.
6 // The definition of this structure is platform-specific.
7 CefMainArgs main_args(GetModuleHandle(nullptr));
8
9 // Optional implementation of the CefApp interface.
10 CefRefPtr app(new SimpleApp);
11
12 // Execute the sub-process logic, if any. This will either return immediately for the browser
13 // process or block until the sub-process should exit.
14 int exit_code = CefExecuteProcess(main_args, app.get(), nullptr);
15 if (exit_code >= 0) {
16 // The sub-process terminated, exit now.
17 return exit_code;
18 }
19
20 // Populate this structure to customize CEF behavior.
21 CefSettings settings;
22 settings.no_sandbox = true;
23
24 // Multi-thread message loop?
25 settings.multi_threaded_message_loop = false;
26
27 // Initialize CEF in the main process.
28 CefInitialize(main_args, settings, app.get(), nullptr);
29
30 // Run the CEF message loop. This will block until CefQuitMessageLoop() is called.
31 CefRunMessageLoop();
32
33 // Shut down CEF.
34 CefShutdown();
35
36 return 0;
37 }
复制代码
其中,打开网址改为百度:www.baidu.com

在这里插入图片描述

构建运行:

在这里插入图片描述

注意:需要将cefsimple demo运行目录下的文件拷贝过来,除了cefsimple外,其余全部拷贝在QtWidgetCef的生成目录下,此外还要包含必须的Qt dll
在这里插入图片描述