zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Sharepoint2013搜索学习笔记之创建搜索服务(二)
    Sharepoint2013搜索学习笔记之设置外网内容源(四)
    C# zxing插件 根据输入的字符串生成二维码
    值类型和引用类型
    Web.config增删查改
    Redis
    2017年最好的6个WEB前端开发手册下载
    php implode()函数详解
    elk日志系统搭建
    aop实现接口请求参数打印
  • 原文地址:https://www.cnblogs.com/MakeView660/p/12163469.html
Copyright © 2011-2022 走看看