Windows下Qt Creator中使用cef(移植cefsimple到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/$${TARGET}/bin else: 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 += $$PWD/include DEPENDPATH += $$PWD/include win32:!win32-g++: PRE_TARGETDEPS += $$PWD/lib/libcef_dll_wrapper.lib else:win32-g++: PRE_TARGETDEPS += $$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<CefApp> 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
如果出现链接错误:libcef_dll_wrapper.lib(browser_view_delegate_cpptoc.obj):-1: error: LNK2001: 无法解析的外部符号 __imp_cef_string_utf16_set等,那是因为没有链接libcef.lib。
测试代码分享:链接: https://pan.baidu.com/s/1G9vzHn8KdcClI55R31gQVA 提取码: nwc5
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类