参考:https://blog.csdn.net/qq_31683775/article/details/84025025
qt中集成cef浏览器例子qtCefBrowser
参考上上篇文章,vs2017编译生成:libcef_dll_wrapper.lib(静态库,debug-MDd,release-MD)
qtCefBrowser工程结构如下:
qt代码如下:
qtCefBrowser.pro
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#------------------------------------------------- # # Project created by QtCreator 2020-01-08T09:18:32 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = cefBrowser # The following define makes your compiler emit warnings if you use SOURCES += main.cpp\ HEADERS += simple_app.h \ CONFIG += debug_and_release FORMS += widget.ui INCLUDEPATH+=$$PWD/cef/ LIBS += shell32.lib \ CONFIG(debug, debug|release) { RC_FILE += icon.rc |
simple_app.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights // reserved. Use of this source code is governed by a BSD-style license that // can be found in the LICENSE file. #ifndef CEF_TESTS_CEFSIMPLE_SIMPLE_APP_H_ #include "include/cef_app.h" // Implement application-level callbacks for the browser process. // CefApp methods: // CefBrowserProcessHandler methods: #endif // CEF_TESTS_CEFSIMPLE_SIMPLE_APP_H_ |
simple_app.cc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights // reserved. Use of this source code is governed by a BSD-style license that // can be found in the LICENSE file. #include "simple_app.h" #include <string> #include "simple_handler.h" SimpleApp::SimpleApp() void SimpleApp::OnContextInitialized() } |
simple_handler.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights // reserved. Use of this source code is governed by a BSD-style license that // can be found in the LICENSE file. #ifndef CEF_TESTS_CEFSIMPLE_SIMPLE_HANDLER_H_ #include "include/cef_client.h" // Provide access to the single global instance of this object. // CefClient methods: // CefLifeSpanHandler methods: // CefLoadHandler methods: // Request that all existing browser windows close. bool IsClosing() const bool is_closing_; // Include the default reference counting implementation. #endif // CEF_TESTS_CEFSIMPLE_SIMPLE_HANDLER_H_ |
simple_handler.cc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights // reserved. Use of this source code is governed by a BSD-style license that // can be found in the LICENSE file. #include "simple_handler.h" #include <sstream> #include "include/base/cef_bind.h" SimpleHandler::SimpleHandler(Widget *w) SimpleHandler::~SimpleHandler() // static void SimpleHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) // Add to the list of existing browsers. bool SimpleHandler::DoClose(CefRefPtr<CefBrowser> browser) // Closing the main window requires special handling. See the DoClose() // Allow the close. For windowed browsers this will result in the OS close void SimpleHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) // Remove from the list of existing browsers. if (browser_list_.empty()) void SimpleHandler::OnLoadError(CefRefPtr<CefBrowser> browser, // Don‘t display an error for downloaded files. // Display a load error message. void SimpleHandler::CloseAllBrowsers(bool force_close) if (browser_list_.empty()) BrowserList::const_iterator it = browser_list_.begin(); |
widget.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> class Widget : public QWidget private: public slots: }; #endif // WIDGET_H |
widget.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include "widget.h" #include "ui_widget.h" #include <QDesktopWidget> Widget::Widget(QWidget *parent) : CefWindowInfo cefWndInfo; QDesktopWidget *pDeskTop = QApplication::desktop(); //将cef界面嵌入qt界面中 CefBrowserSettings cefBrowSetting; Widget::~Widget() CefRefPtr<CefBrowser> Widget::GetBrowserByID(int nWebBrowserID) return nullptr; void Widget::onUrl() |
widget.ui
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Widget</class> <widget class="QWidget" name="Widget"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>711</width> <height>371</height> </rect> </property> <property name="windowTitle"> <string>qt cef browser</string> </property> <layout class="QVBoxLayout" name="verticalLayout"> <property name="spacing"> <number>0</number> </property> <property name="leftMargin"> <number>0</number> </property> <property name="topMargin"> <number>0</number> </property> <property name="rightMargin"> <number>0</number> </property> <property name="bottomMargin"> <number>0</number> </property> <item> <widget class="QWidget" name="toolbar" native="true"> <property name="minimumSize"> <size> <width>0</width> <height>40</height> </size> </property> <property name="maximumSize"> <size> <width>16777215</width> <height>40</height> </size> </property> <layout class="QHBoxLayout" name="horizontalLayout"> <property name="spacing"> <number>0</number> </property> <property name="leftMargin"> <number>0</number> </property> <property name="topMargin"> <number>0</number> </property> <property name="rightMargin"> <number>0</number> </property> <property name="bottomMargin"> <number>0</number> </property> <item> <widget class="QLineEdit" name="lineEdit"> <property name="minimumSize"> <size> <width>0</width> <height>40</height> </size> </property> <property name="maximumSize"> <size> <width>16777215</width> <height>40</height> </size> </property> <property name="styleSheet"> <string notr="true">font-size:16px;</string> </property> <property name="text"> <string>http://www.baidu.com</string> </property> <property name="placeholderText"> <string>Please input URL...</string> </property> <property name="clearButtonEnabled"> <bool>true</bool> </property> </widget> </item> <item> <widget class="QPushButton" name="goButton"> <property name="minimumSize"> <size> <width>0</width> <height>40</height> </size> </property> <property name="maximumSize"> <size> <width>16777215</width> <height>40</height> </size> </property> <property name="styleSheet"> <string notr="true">font-size:15px; font: 16pt "Arial";</string> </property> <property name="text"> <string>Go</string> </property> </widget> </item> </layout> </widget> </item> <item> <widget class="QWidget" name="widget" native="true"/> </item> </layout> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui> |
构建运行:
原文地址:https://www.cnblogs.com/MakeView660/p/12166287.html