修改部分代码

This commit is contained in:
JackLee 2025-03-20 17:35:09 +08:00
parent 02b34502e1
commit 3181624374
6 changed files with 39 additions and 261 deletions

View File

@ -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---------------")
#suicpp
FILE(GLOB src_sui "${PROJECT_SOURCE_DIR}/src/*.*")
FILE(GLOB browser "${PROJECT_SOURCE_DIR}/src/cef/*.*")
SET(PROJECT_SOURCES ${browser} ${src_sui})
#suicpp
set(SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src")
#
FILE(GLOB_RECURSE SOURCES "${SOURCE_DIR}/*.*")
SET(PROJECT_SOURCES ${SOURCES})
# qFatalqDebug
#LIST(APPEND CMAKE_CXX_FLAGS "-DQT_NO_DEBUG_OUTPUT")

View File

@ -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);
}

View File

@ -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

View File

@ -8,126 +8,22 @@
#include <QJsonValue>
#include <QMessageBox>
#include <QRandomGenerator>
#include <QCefContext.h>
#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

View File

@ -2,28 +2,22 @@
#define QCEFVIEWTEST_H
#include <QCloseEvent>
#include <QMainWindow>
#include "ui_MainWindow.h"
#include <QWidget>
#include <QPushButton>
#include "CefViewWidget.h"
class MainWindow : public QMainWindow
#include <QVBoxLayout>
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

View File

@ -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));