From 2eb99063701f0cdc3199778ace189dd5476bb4fd Mon Sep 17 00:00:00 2001 From: cbwu <504-wuchengbo@htsdfp.com> Date: Thu, 5 Sep 2024 09:44:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BD=91=E9=A1=B5=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=8F=B3=E9=94=AE=E5=AF=BC=E8=88=AA=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Src/WebPage/customwebenginepage.cpp | 34 +++++++------- Src/WebPage/cwebengineview.cpp | 73 +++++++++++++++++++++++++++-- Src/WebPage/cwebengineview.h | 11 +++++ main.cpp | 33 +++++++------ 4 files changed, 115 insertions(+), 36 deletions(-) diff --git a/Src/WebPage/customwebenginepage.cpp b/Src/WebPage/customwebenginepage.cpp index 053d300..b04358c 100644 --- a/Src/WebPage/customwebenginepage.cpp +++ b/Src/WebPage/customwebenginepage.cpp @@ -1,22 +1,24 @@ #include "customwebenginepage.h" - -CustomWebEnginePage::CustomWebEnginePage(QObject *parent/* = Q_NULLPTR*/) - : QWebEnginePage(parent) -{ - connect(this, &CustomWebEnginePage::featurePermissionRequested, this, &CustomWebEnginePage::onFeaturePermissionRequested); +#include +CustomWebEnginePage::CustomWebEnginePage(QObject *parent /* = Q_NULLPTR*/) + : QWebEnginePage(parent) { + connect(this, &CustomWebEnginePage::featurePermissionRequested, this, + &CustomWebEnginePage::onFeaturePermissionRequested); } -bool CustomWebEnginePage::certificateError(const QWebEngineCertificateError &certificateError) -{ - return true; +bool CustomWebEnginePage::certificateError( + const QWebEngineCertificateError &certificateError) { + return true; } -void CustomWebEnginePage::onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature) -{ - if (feature == QWebEnginePage::MediaAudioCapture - || feature == QWebEnginePage::MediaVideoCapture - || feature == QWebEnginePage::MediaAudioVideoCapture) - setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser); - else - setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionDeniedByUser); +void CustomWebEnginePage::onFeaturePermissionRequested( + const QUrl &securityOrigin, QWebEnginePage::Feature feature) { + if (feature == QWebEnginePage::MediaAudioCapture || + feature == QWebEnginePage::MediaVideoCapture || + feature == QWebEnginePage::MediaAudioVideoCapture) + setFeaturePermission(securityOrigin, feature, + QWebEnginePage::PermissionGrantedByUser); + else + setFeaturePermission(securityOrigin, feature, + QWebEnginePage::PermissionDeniedByUser); } diff --git a/Src/WebPage/cwebengineview.cpp b/Src/WebPage/cwebengineview.cpp index e6a9f8d..8b241c1 100644 --- a/Src/WebPage/cwebengineview.cpp +++ b/Src/WebPage/cwebengineview.cpp @@ -1,11 +1,41 @@ #include "cwebengineview.h" #include "CWebEngineView.h" - -#include #include -CWebEngineView::CWebEngineView(QWidget *parent) : QWebEngineView(parent) {} +CWebEngineView::CWebEngineView(QWidget *parent) : QWebEngineView(parent) { + // 记录首页URL + connect( + this, &QWebEngineView::loadFinished, this, + [&](bool ok) { + if (ok) { + firstURL = page()->url(); + } + }, + Qt::SingleShotConnection); + // 获取当前页面的profile + QWebEngineProfile *profile = page()->profile(); + + // 启用缓存 + profile->setHttpCacheType(QWebEngineProfile::MemoryHttpCache); + profile->setPersistentCookiesPolicy( + QWebEngineProfile::ForcePersistentCookies); + + // 获取设置对象 + QWebEngineSettings *settings = page()->settings(); + + // 启用硬件加速 + settings->setAttribute(QWebEngineSettings::Accelerated2dCanvasEnabled, true); + settings->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, true); + + // 其他设置 + settings->setAttribute(QWebEngineSettings::JavascriptEnabled, true); + settings->setAttribute(QWebEngineSettings::PluginsEnabled, true); + settings->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, + true); + settings->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, + true); +} CWebEngineView::~CWebEngineView() { QWebEnginePage *page = this->page(); @@ -20,6 +50,43 @@ CWebEngineView::createWindow(QWebEnginePage::WebWindowType type) { QWebEnginePage *new_page = new QWebEnginePage(this->page()->profile(), this); this->setPage(new_page); + connect(new_page, &QWebEnginePage::destroyed, new_page, + [new_page]() { new_page->deleteLater(); }); + // connect(page(), &QWebEnginePage::urlChanged, this, [&](const QUrl &url) { + // qDebug() << "**********GoTo URL:" << url.toString(); + // }); } return this; } + +void CWebEngineView::contextMenuEvent(QContextMenuEvent *event) { + // 创建一个新的菜单 + QMenu menu(this); + menu.setStyleSheet( + "QMenu{background-color: white;color: black;}QMenu::item:selected " + "{background-color: lightgray;color: black;}"); + // 添加自定义菜单项 + QAction *forwardAction = menu.addAction("前进"); + QAction *backAction = menu.addAction("后退"); + QAction *reloadAction = menu.addAction("刷新"); + // 显示菜单 + QAction *action = menu.exec(event->globalPos()); + if (action) { + QWebEnginePage *page = this->page(); + if (action == forwardAction) { + if (page->history()->canGoForward()) { + page->triggerAction(QWebEnginePage::Forward); + } + } else if (action == backAction) { + if (page->history()->canGoBack()) { + page->triggerAction(QWebEnginePage::Back); + } else if (page->url() == page->history()->items()[0].url()) { + page->load(firstURL); + } + // qDebug() << "**********History URL:" + // << this->page()->history()->items()[0].url().toString(); + } else if (action == reloadAction) { + page->triggerAction(QWebEnginePage::Reload); + } + } +} diff --git a/Src/WebPage/cwebengineview.h b/Src/WebPage/cwebengineview.h index d90b4be..91cb63a 100644 --- a/Src/WebPage/cwebengineview.h +++ b/Src/WebPage/cwebengineview.h @@ -1,6 +1,12 @@ #ifndef CWEBENGINEVIEW_H #define CWEBENGINEVIEW_H +#include +#include +#include +#include +#include +#include #include class QWidget; @@ -15,6 +21,11 @@ public: protected: // 解决错误:qt No RenderWidgetHost exists with id 10 in process 3 virtual QWebEngineView *createWindow(QWebEnginePage::WebWindowType type); + // 自定义右键菜单 + void contextMenuEvent(QContextMenuEvent *event) override; + +private: + QUrl firstURL; }; #endif // CWEBENGINEVIEW_H diff --git a/main.cpp b/main.cpp index 216e0dd..1243f0f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,26 +1,25 @@ #include "mainwindow.h" #include +#include #include #include -#include - -int main(int argc, char *argv[]) -{ - //QApplication::setAttribute(Qt::AA_UseOpenGLES); - QApplication a(argc, argv); - QTranslator translator; - const QStringList uiLanguages = QLocale::system().uiLanguages(); - for (const QString &locale : uiLanguages) { - const QString baseName = "PayloadAPP_" + QLocale(locale).name(); - if (translator.load(":/i18n/" + baseName)) { - a.installTranslator(&translator); - break; - } +int main(int argc, char *argv[]) { + // QApplication::setAttribute(Qt::AA_UseOpenGLES); + // qputenv("QTWEBENGINE_CHROMIUM_FLAGS", "--enable-gpu-rasterization"); + QApplication a(argc, argv); + QTranslator translator; + const QStringList uiLanguages = QLocale::system().uiLanguages(); + for (const QString &locale : uiLanguages) { + const QString baseName = "PayloadAPP_" + QLocale(locale).name(); + if (translator.load(":/i18n/" + baseName)) { + a.installTranslator(&translator); + break; } + } - MainWindow w; - w.show(); - return a.exec(); + MainWindow w; + w.show(); + return a.exec(); }