From 31816243748a54e4aecceb2ce0684a18c34bea79 Mon Sep 17 00:00:00 2001 From: JackLee <809262979@qq.com> Date: Thu, 20 Mar 2025 17:35:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 11 +- src/Cef/CefViewWidget.cpp | 31 +----- src/Cef/CefViewWidget.h | 9 -- src/Cef/MainWindow.cpp | 205 ++++---------------------------------- src/Cef/MainWindow.h | 40 ++------ src/main.cpp | 4 +- 6 files changed, 39 insertions(+), 261 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5457050..53f12ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,6 +72,7 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/3rdparty/MicroTeX) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/MicroTeX/src) add_definitions(-DQCEFVIEW_STATIC) +add_definitions(-DCEF_USE_QT_EVENT_LOOP) #libcef_dll_wrapper include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/libcef_dll_wrapper) link_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/libcef_dll_wrapper/lib) @@ -92,10 +93,12 @@ MESSAGE(STATUS "QT_DIR_PATH=${QT_DIR}") MESSAGE(STATUS "JSON_DIR_PATH=${JSON}") MESSAGE(STATUS "---------- END---------------") -#增加sui的cpp -FILE(GLOB src_sui "${PROJECT_SOURCE_DIR}/src/*.*") -FILE(GLOB browser "${PROJECT_SOURCE_DIR}/src/cef/*.*") -SET(PROJECT_SOURCES ${browser} ${src_sui}) +#增加sui的cpp定义要搜索源文件的根目录 +set(SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src") + +# 自动获取指定目录及其子目录中的所有源文件 +FILE(GLOB_RECURSE SOURCES "${SOURCE_DIR}/*.*") +SET(PROJECT_SOURCES ${SOURCES}) # 设置消息策略为qFatal来禁用qDebug信息 #LIST(APPEND CMAKE_CXX_FLAGS "-DQT_NO_DEBUG_OUTPUT") diff --git a/src/Cef/CefViewWidget.cpp b/src/Cef/CefViewWidget.cpp index bf1b892..d88e82c 100644 --- a/src/Cef/CefViewWidget.cpp +++ b/src/Cef/CefViewWidget.cpp @@ -19,9 +19,6 @@ CefViewWidget::CefViewWidget(const QString url, const QCefSetting* setting, QWidget* parent /* = 0*/) : QCefView(url, setting, parent) { - // setStyleSheet("background: blue;"); - - connect(this, &CefViewWidget::draggableRegionChanged, this, &CefViewWidget::onDraggableRegionChanged); connect(this, &CefViewWidget::nativeBrowserCreated, this, &CefViewWidget::onNativeBrowserWindowCreated); } @@ -48,13 +45,6 @@ CefViewWidget::onNativeBrowserWindowCreated(QWindow* window) updateMask(); } -void -CefViewWidget::onDraggableRegionChanged(const QRegion& draggableRegion, const QRegion& nonDraggableRegion) -{ - m_draggableRegion = draggableRegion; - m_nonDraggableRegion = nonDraggableRegion; -} - bool CefViewWidget::onNewPopup(const QCefFrameId& sourceFrameId, const QString& targetUrl, @@ -100,29 +90,10 @@ void CefViewWidget::mousePressEvent(QMouseEvent* event) { QCefView::mousePressEvent(event); - -#if defined(Q_OS_WIN) - if (event->buttons().testFlag(Qt::LeftButton) && m_draggableRegion.contains(event->pos())) { - HWND hWnd = ::GetAncestor((HWND)(window()->windowHandle()->winId()), GA_ROOT); - POINT pt; - ::GetCursorPos(&pt); - ::ReleaseCapture(); - ::SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, POINTTOPOINTS(pt)); - } -#elif defined(Q_OS_MAC) -#elif defined(Q_OS_LINUX) -#else -#endif } void CefViewWidget::updateMask() { - // create a rect with rounded corner (50px radius) as mask - QPainterPath path; - path.addRoundedRect(rect(), 50, 50); - QRegion mask = QRegion(path.toFillPolygon().toPolygon()); - - // apply the mask - setMask(mask); + } diff --git a/src/Cef/CefViewWidget.h b/src/Cef/CefViewWidget.h index 7c25899..32cb473 100644 --- a/src/Cef/CefViewWidget.h +++ b/src/Cef/CefViewWidget.h @@ -21,9 +21,6 @@ protected slots: void onScreenChanged(QScreen* screen); void onNativeBrowserWindowCreated(QWindow* window); - - void onDraggableRegionChanged(const QRegion& draggableRegion, const QRegion& nonDraggableRegion); - protected: bool onNewPopup(const QCefFrameId& sourceFrameId, const QString& targetUrl, @@ -47,12 +44,6 @@ private: private: QWindow* m_pCefWindow = nullptr; - - int m_iCornerRadius = 50; - - QRegion m_draggableRegion; - - QRegion m_nonDraggableRegion; }; #endif // CUSTOMCEFVIEW_H diff --git a/src/Cef/MainWindow.cpp b/src/Cef/MainWindow.cpp index f61893b..3a61cd5 100644 --- a/src/Cef/MainWindow.cpp +++ b/src/Cef/MainWindow.cpp @@ -8,126 +8,22 @@ #include #include #include - #include - -#define URL_ROOT "http://QCefViewDoc" -#define LEFT_INDEX_URL URL_ROOT "/left.html" -#define RIGHT_INDEX_URL URL_ROOT "/right.html" - MainWindow::MainWindow(QWidget* parent) - : QMainWindow(parent /*, Qt::FramelessWindowHint*/) + : QWidget(parent /*, Qt::FramelessWindowHint*/) { - m_ui.setupUi(this); - -#ifdef Q_OS_MACOS - this->m_ui.nativeContainer->setContentsMargins(0, 28, 0, 0); -#endif - - setupWindow(); - - // setWindowFlags(Qt::FramelessWindowHint); - // setAttribute(Qt::WA_TranslucentBackground); - - connect(m_ui.btn_showDevTools, &QPushButton::clicked, this, &MainWindow::onBtnShowDevToolsClicked); - connect(m_ui.btn_reloadRight, &QPushButton::clicked, this, &MainWindow::onBtnReloadRightViewClicked); - connect(m_ui.btn_recreateRight, &QPushButton::clicked, this, &MainWindow::onBtnRecreateRightViewClicked); - connect(m_ui.btn_changeColor, &QPushButton::clicked, this, &MainWindow::onBtnChangeColorClicked); - connect(m_ui.btn_setFocus, &QPushButton::clicked, this, &MainWindow::onBtnSetFocusClicked); - connect(m_ui.btn_callJSCode, &QPushButton::clicked, this, &MainWindow::onBtnCallJSCodeClicked); - connect(m_ui.btn_newBrowser, &QPushButton::clicked, this, &MainWindow::onBtnNewBrowserClicked); - connect(m_ui.btn_quitApp, &QPushButton::clicked, qApp, &QCoreApplication::quit); - - // build the path to the web resource - QDir dir = QCoreApplication::applicationDirPath(); -#if defined(Q_OS_MACOS) - QString webResourceDir = /*QString("file://") +*/ QDir::toNativeSeparators(dir.filePath("../Resources/webres")); -#else - QString webResourceDir = /*QString("file://") +*/ QDir::toNativeSeparators(dir.filePath("webres")); -#endif - - // add a local folder to URL map (global) - QCefContext::instance()->addLocalFolderResource(webResourceDir, URL_ROOT); - - createLeftCefView(); - createRightCefView(); + m_layout=new QVBoxLayout(); + m_newbrowser=new QPushButton(tr("新浏览器窗口")); + m_exit=new QPushButton(tr("退出")); + m_layout->addWidget(m_newbrowser); + m_layout->addWidget(m_exit); + setLayout(m_layout); + connect(m_newbrowser, &QPushButton::clicked, this, &MainWindow::onBtnNewBrowserClicked); + connect(m_exit, &QPushButton::clicked, qApp, &QCoreApplication::quit); } MainWindow::~MainWindow() {} -void -MainWindow::createLeftCefView() -{ - if (m_pLeftCefViewWidget) { - m_pLeftCefViewWidget->deleteLater(); - m_pLeftCefViewWidget = nullptr; - } - - QCefSetting setting; - setting.setWindowlessFrameRate(1000); - setting.setHardwareAcceleration(false); - // setting.setBackgroundColor(Qt::magenta); - - m_pLeftCefViewWidget = new CefViewWidget("https://www.testufo.com/", &setting, this); - // connect the invokeMethod to the slot - connect(m_pLeftCefViewWidget, &QCefView::invokeMethod, this, &MainWindow::onInvokeMethod); - - // connect the cefQueryRequest to the slot - connect(m_pLeftCefViewWidget, &QCefView::cefUrlRequest, this, &MainWindow::onQCefUrlRequest); - connect(m_pLeftCefViewWidget, &QCefView::cefQueryRequest, this, &MainWindow::onQCefQueryRequest); - connect(m_pLeftCefViewWidget, &QCefView::reportJavascriptResult, this, &MainWindow::onJavascriptResult); - connect(m_pLeftCefViewWidget, &QCefView::loadStart, this, &MainWindow::onLoadStart); - connect(m_pLeftCefViewWidget, &QCefView::loadEnd, this, &MainWindow::onLoadEnd); - connect(m_pLeftCefViewWidget, &QCefView::loadError, this, &MainWindow::onLoadError); - - m_ui.leftCefViewContainer->layout()->addWidget(m_pLeftCefViewWidget); -} - -void -MainWindow::createRightCefView() -{ - if (m_pRightCefViewWidget) { - m_pRightCefViewWidget->deleteLater(); - m_pRightCefViewWidget = nullptr; - } - - // build settings for per QCefView - QCefSetting setting; -#if CEF_VERSION_MAJOR < 100 - setting.setPlugins(false); -#endif - setting.setWindowlessFrameRate(1000); - setting.setHardwareAcceleration(true); - QColor background(0, 255, 0, 255); - setting.setBackgroundColor(background); - - // create the QCefView widget and add it to the layout container - // m_pRightCefViewWidget = new QCefView(RIGHT_INDEX_URL, &setting, this); - m_pRightCefViewWidget = new QCefView("https://www.testufo.com/", &setting, this); - - // auto vl = new QVBoxLayout(m_pRightCefViewWidget); - // auto btn = new QPushButton("TEST BUTTON OVERLAY", m_pRightCefViewWidget); - //// btn->setFixedSize(320, 240); - // btn->setStyleSheet("background-color: rgba(1, 1, 1, 0);"); - // btn->setAttribute(Qt::WA_TranslucentBackground); - // btn->setWindowFlags(Qt::FramelessWindowHint); - // btn->setAttribute(Qt::WA_NoSystemBackground); - // vl->setAlignment(Qt::AlignVCenter); - - // vl->addWidget(btn); - // m_pRightCefViewWidget->setLayout(vl); - - // all the following values will disable the context menu for both NCW and OSR mode - // m_pRightCefViewWidget->setContextMenuPolicy(Qt::NoContextMenu); - // m_pRightCefViewWidget->setContextMenuPolicy(Qt::ActionsContextMenu); - // m_pRightCefViewWidget->setContextMenuPolicy(Qt::CustomContextMenu); - // m_pRightCefViewWidget->setContextMenuPolicy(Qt::PreventContextMenu); - m_pRightCefViewWidget->setContextMenuPolicy(Qt::DefaultContextMenu); - - // add the QCefView widget to the layout - m_ui.rightCefViewContainer->layout()->addWidget(m_pRightCefViewWidget); -} - void MainWindow::onInvokeMethod(const QCefBrowserId& browserId, const QCefFrameId& frameId, @@ -182,12 +78,7 @@ MainWindow::onQCefQueryRequest(const QCefBrowserId& browserId, const QCefFrameId QString text = QString("Current Thread: QT_UI\r\n" "Query: %1") .arg(query.request()); - QMessageBox::information(this->window(), title, text); - - QString response = query.request().toUpper(); - query.setResponseResult(true, response); - m_pLeftCefViewWidget->responseQCefQuery(query); } void @@ -241,77 +132,23 @@ MainWindow::onLoadError(const QCefBrowserId& browserId, << ", errorCode:" << errorCode; } -void -MainWindow::onBtnShowDevToolsClicked() -{ - if (m_pLeftCefViewWidget) { - m_pLeftCefViewWidget->showDevTools(); - } -} - -void -MainWindow::onBtnReloadRightViewClicked() -{ - if (m_pRightCefViewWidget) { - m_pRightCefViewWidget->navigateToUrl("https://www.google.com"); - } -} - -void -MainWindow::onBtnRecreateRightViewClicked() -{ - createRightCefView(); -} - -void -MainWindow::onBtnChangeColorClicked() -{ - if (m_pLeftCefViewWidget) { - // create a random color - QColor color(QRandomGenerator::global()->generate()); - - // create the cef event and set the arguments - QCefEvent event("colorChange"); - event.arguments().append(QVariant::fromValue(color.name(QColor::HexArgb))); - - // broadcast the event to all frames in all browsers created by this QCefView widget - m_pLeftCefViewWidget->broadcastEvent(event); - } -} - -void -MainWindow::onBtnCallJSCodeClicked() -{ - QString context = "helloQCefView"; - QString code = "alert('hello QCefView'); return {k1: 'str', k2: true, k3: 100};"; - m_pLeftCefViewWidget->executeJavascriptWithResult(QCefView::MainFrameID, code, "", context); -} - -void -MainWindow::onBtnSetFocusClicked() -{ - if (m_pLeftCefViewWidget) { - m_pLeftCefViewWidget->setFocus(); - } -} - void MainWindow::onBtnNewBrowserClicked() { - QMainWindow* w = new QMainWindow(nullptr); + QWidget* w = new QWidget(); w->setAttribute(Qt::WA_DeleteOnClose); - + QVBoxLayout* m_layout=new QVBoxLayout(); QCefSetting settings; - QCefView* view = new QCefView("https://cefview.github.io/QCefView/", &settings, w); - - w->setCentralWidget(view); + CefViewWidget* view = new CefViewWidget("https://wwww.baidu.com/", &settings, w); + connect(view, &QCefView::invokeMethod, this, &MainWindow::onInvokeMethod); + connect(view, &QCefView::cefUrlRequest, this, &MainWindow::onQCefUrlRequest); + connect(view, &QCefView::cefQueryRequest, this, &MainWindow::onQCefQueryRequest); + connect(view, &QCefView::reportJavascriptResult, this, &MainWindow::onJavascriptResult); + connect(view, &QCefView::loadStart, this, &MainWindow::onLoadStart); + connect(view, &QCefView::loadEnd, this, &MainWindow::onLoadEnd); + connect(view, &QCefView::loadError, this, &MainWindow::onLoadError); + m_layout->addWidget(view); + w->setLayout(m_layout); w->resize(1024, 768); w->show(); } - -#ifndef Q_OS_MACOS -void -MainWindow::setupWindow() -{ -} -#endif diff --git a/src/Cef/MainWindow.h b/src/Cef/MainWindow.h index 019e16c..61c7719 100644 --- a/src/Cef/MainWindow.h +++ b/src/Cef/MainWindow.h @@ -2,28 +2,22 @@ #define QCEFVIEWTEST_H #include -#include - -#include "ui_MainWindow.h" - +#include +#include #include "CefViewWidget.h" - -class MainWindow : public QMainWindow +#include +class MainWindow : public QWidget { Q_OBJECT public: MainWindow(QWidget* parent = 0); ~MainWindow(); - -protected: - void createLeftCefView(); - void createRightCefView(); - void setupWindow(); - - // QCefView slots +private: + QVBoxLayout* m_layout={}; + QPushButton* m_newbrowser={}; + QPushButton* m_exit={}; protected slots: - void onInvokeMethod(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QString& method, @@ -53,25 +47,7 @@ protected slots: // ui slots protected slots: - void onBtnShowDevToolsClicked(); - - void onBtnReloadRightViewClicked(); - - void onBtnRecreateRightViewClicked(); - - void onBtnChangeColorClicked(); - - void onBtnSetFocusClicked(); - - void onBtnCallJSCodeClicked(); - void onBtnNewBrowserClicked(); - -private: - Ui::MainWindow m_ui; - - QCefView* m_pLeftCefViewWidget = nullptr; - QCefView* m_pRightCefViewWidget = nullptr; }; #endif // QCEFVIEWTEST_H diff --git a/src/main.cpp b/src/main.cpp index 3251c09..18d073c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -76,10 +76,10 @@ int main(int argc, char *argv[]) // config.addCommandLineSwitch("disable-spell-checking"); // config.addCommandLineSwitch("disable-site-isolation-trials"); // config.addCommandLineSwitch("enable-aggressive-domstorage-flushing"); - config.addCommandLineSwitchWithValue("renderer-process-limit", "1"); + config.addCommandLineSwitchWithValue("renderer-process-limit", "3"); // allow remote debugging config.addCommandLineSwitchWithValue("remote-allow-origins", "*"); - // config.addCommandLineSwitchWithValue("disable-features", "BlinkGenPropertyTrees,TranslateUI,site-per-process"); + config.addCommandLineSwitchWithValue("disable-features", "BlinkGenPropertyTrees,TranslateUI,site-per-process"); // set cache folder config.setCachePath(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation));