修改主窗口

This commit is contained in:
JackLee 2025-03-21 17:44:44 +08:00
parent c3ec270b3b
commit fa8f19f174
3 changed files with 125 additions and 356 deletions

View File

@ -11,64 +11,90 @@
#include <QCefContext.h>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent /*, Qt::FramelessWindowHint*/)
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent /*, Qt::FramelessWindowHint*/)
{
m_ui.setupUi(this);
connect(m_ui.btn_showDevTools, &QPushButton::clicked, this, &MainWindow::onBtnShowDevToolsClicked);
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);
createLeftCefView();
m_Layout = new QVBoxLayout;
m_OptionsLayout = new QHBoxLayout;
m_BrowserLayout = new QHBoxLayout;
m_NewBrowser = new QPushButton(tr("New Browser"));
m_ExitAllBrowser = new QPushButton(tr("Exit ALL Browser"));
m_DevTools = new QPushButton(tr("DevTools"));
m_CallJSCode = new QPushButton(tr("Call JS Code"));
m_SetFocus = new QPushButton(tr("Set Focus"));
m_BtnToUrl = new QPushButton(tr("To Url"));
m_TextToUrl = new QLineEdit();
m_TextToUrl->setPlaceholderText(tr("输入前往url地址"));
m_OptionsLayout->addWidget(m_NewBrowser);
m_OptionsLayout->addWidget(m_TextToUrl);
m_OptionsLayout->addWidget(m_BtnToUrl);
m_OptionsLayout->addWidget(m_SetFocus);
m_OptionsLayout->addWidget(m_CallJSCode);
m_OptionsLayout->addWidget(m_DevTools);
m_OptionsLayout->addWidget(m_ExitAllBrowser);
m_OptionsLayout->setStretch(1,7);
m_Layout->addLayout(m_OptionsLayout);
m_Layout->addLayout(m_BrowserLayout);
m_Layout->setStretch(0,2);
m_Layout->setStretch(1,8);
setLayout(m_Layout);
connect(m_BtnToUrl, &QPushButton::clicked, this, &MainWindow::onBtnToUrl);
connect(m_DevTools, &QPushButton::clicked, this, &MainWindow::onBtnShowDevToolsClicked);
connect(m_SetFocus, &QPushButton::clicked, this, &MainWindow::onBtnSetFocusClicked);
connect(m_CallJSCode, &QPushButton::clicked, this, &MainWindow::onBtnCallJSCodeClicked);
connect(m_NewBrowser, &QPushButton::clicked, this, &MainWindow::onBtnNewBrowserClicked);
connect(m_ExitAllBrowser, &QPushButton::clicked, qApp, &QCoreApplication::quit);
initBrowser();
}
MainWindow::~MainWindow() {}
void
MainWindow::createLeftCefView()
void MainWindow::initBrowser()
{
if (m_pLeftCefViewWidget) {
m_pLeftCefViewWidget->deleteLater();
m_pLeftCefViewWidget = nullptr;
if (m_CefWidget)
{
m_CefWidget->deleteLater();
m_CefWidget = nullptr;
}
setting.setHardwareAcceleration(true);
m_CefWidget = new CefViewWidget("https://www.bilibili.com/", &setting, this);
connect(m_CefWidget, &QCefView::invokeMethod, this, &MainWindow::onInvokeMethod);
connect(m_CefWidget, &QCefView::cefUrlRequest, this, &MainWindow::onQCefUrlRequest);
connect(m_CefWidget, &QCefView::cefQueryRequest, this, &MainWindow::onQCefQueryRequest);
connect(m_CefWidget, &QCefView::reportJavascriptResult, this, &MainWindow::onJavascriptResult);
connect(m_CefWidget, &QCefView::loadStart, this, &MainWindow::onLoadStart);
connect(m_CefWidget, &QCefView::loadEnd, this, &MainWindow::onLoadEnd);
connect(m_CefWidget, &QCefView::loadError, this, &MainWindow::onLoadError);
m_BrowserLayout->addWidget(m_CefWidget);
}
void MainWindow::onBtnToUrl()
{
QCefSetting setting;
setting.setWindowlessFrameRate(1000);
setting.setHardwareAcceleration(false);
// setting.setBackgroundColor(Qt::magenta);
m_pLeftCefViewWidget = new CefViewWidget("https://www.bilibili.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);
if (!m_TextToUrl->text().isEmpty())
{
m_CefWidget->navigateToUrl(m_TextToUrl->text());
}
}
void
MainWindow::onInvokeMethod(const QCefBrowserId& browserId,
const QCefFrameId& frameId,
const QString& method,
const QVariantList& arguments)
void MainWindow::onInvokeMethod(const QCefBrowserId &browserId,
const QCefFrameId &frameId,
const QString &method,
const QVariantList &arguments)
{
// extract the arguments and dispatch the invocation to corresponding handler
if (0 == method.compare("TestMethod")) {
if (0 == method.compare("TestMethod"))
{
QString title("QCef InvokeMethod Notify");
QString text = QString("================== Current Thread: QT_UI ==================\r\n"
"Frame: %1\r\n"
"Method: %2\r\n"
"Arguments:\r\n")
.arg(frameId)
.arg(method);
.arg(frameId)
.arg(method);
for (int i = 0; i < arguments.size(); i++) {
for (int i = 0; i < arguments.size(); i++)
{
auto jv = QJsonValue::fromVariant(arguments[i]);
// clang-format off
@ -84,41 +110,40 @@ MainWindow::onInvokeMethod(const QCefBrowserId& browserId,
text.append(QString("\r\nArguments List in JSON format:\r\n%1").arg(jsonString));
QMessageBox::information(this->window(), title, text);
} else {
}
else
{
}
}
void
MainWindow::onQCefUrlRequest(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QString& url)
void MainWindow::onQCefUrlRequest(const QCefBrowserId &browserId, const QCefFrameId &frameId, const QString &url)
{
QString title("QCef URL Request");
QString text = QString("Current Thread: QT_UI\r\n"
"URL: %1")
.arg(url);
.arg(url);
QMessageBox::information(this->window(), title, text);
}
void
MainWindow::onQCefQueryRequest(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QCefQuery& query)
void MainWindow::onQCefQueryRequest(const QCefBrowserId &browserId, const QCefFrameId &frameId, const QCefQuery &query)
{
QString title("QCef Query Request");
QString text = QString("Current Thread: QT_UI\r\n"
"Query: %1")
.arg(query.request());
.arg(query.request());
QMessageBox::information(this->window(), title, text);
QString response = query.request().toUpper();
query.setResponseResult(true, response);
m_pLeftCefViewWidget->responseQCefQuery(query);
m_CefWidget->responseQCefQuery(query);
}
void
MainWindow::onJavascriptResult(const QCefBrowserId& browserId,
const QCefFrameId& frameId,
const QString& context,
const QVariant& result)
void MainWindow::onJavascriptResult(const QCefBrowserId &browserId,
const QCefFrameId &frameId,
const QString &context,
const QVariant &result)
{
auto jsonValue = QJsonDocument::fromVariant(result);
auto jsonString = QString(jsonValue.toJson());
@ -129,77 +154,60 @@ MainWindow::onJavascriptResult(const QCefBrowserId& browserId,
QMessageBox::information(this->window(), title, text);
}
void
MainWindow::onLoadingStateChanged(const QCefBrowserId& browserId, bool isLoading, bool canGoBack, bool canGoForward)
void MainWindow::onLoadingStateChanged(const QCefBrowserId &browserId, bool isLoading, bool canGoBack, bool canGoForward)
{
qDebug() << "onLoadingStateChanged, browserId:" << browserId << ", isLoading:" << isLoading
<< ", canGoBack:" << canGoBack << ", canGoForward:" << canGoForward;
}
void
MainWindow::onLoadStart(const QCefBrowserId& browserId,
const QCefFrameId& frameId,
bool isMainFrame,
int transitionType)
void MainWindow::onLoadStart(const QCefBrowserId &browserId,
const QCefFrameId &frameId,
bool isMainFrame,
int transitionType)
{
qDebug() << "onLoadStart, browserId:" << browserId << ", frameId:" << frameId << ", isMainFrame:" << isMainFrame
<< ", transitionType:" << transitionType;
}
void
MainWindow::onLoadEnd(const QCefBrowserId& browserId, const QCefFrameId& frameId, bool isMainFrame, int httpStatusCode)
void MainWindow::onLoadEnd(const QCefBrowserId &browserId, const QCefFrameId &frameId, bool isMainFrame, int httpStatusCode)
{
qDebug() << "onLoadEnd, browserId:" << browserId << ", frameId:" << frameId << ", isMainFrame:" << isMainFrame
<< ", httpStatusCode:" << httpStatusCode;
}
void
MainWindow::onLoadError(const QCefBrowserId& browserId,
const QCefFrameId& frameId,
bool isMainFrame,
int errorCode,
const QString& errorMsg,
const QString& failedUrl)
void MainWindow::onLoadError(const QCefBrowserId &browserId,
const QCefFrameId &frameId,
bool isMainFrame,
int errorCode,
const QString &errorMsg,
const QString &failedUrl)
{
qDebug() << "onLoadError, browserId:" << browserId << ", frameId:" << frameId << ", isMainFrame:" << isMainFrame
<< ", errorCode:" << errorCode;
}
void
MainWindow::onBtnShowDevToolsClicked()
void MainWindow::onBtnShowDevToolsClicked()
{
if (m_pLeftCefViewWidget) {
m_pLeftCefViewWidget->showDevTools();
if (m_CefWidget)
{
m_CefWidget->showDevTools();
}
}
void
MainWindow::onBtnCallJSCodeClicked()
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);
m_CefWidget->executeJavascriptWithResult(QCefView::MainFrameID, code, "", context);
}
void
MainWindow::onBtnSetFocusClicked()
void MainWindow::onBtnSetFocusClicked()
{
if (m_pLeftCefViewWidget) {
m_pLeftCefViewWidget->setFocus();
if (m_CefWidget)
{
m_CefWidget->setFocus();
}
}
void
MainWindow::onBtnNewBrowserClicked()
void MainWindow::onBtnNewBrowserClicked()
{
QMainWindow* w = new QMainWindow(nullptr);
w->setAttribute(Qt::WA_DeleteOnClose);
QCefSetting settings;
QCefView* view = new QCefView("https://www.bilibili.com/", &settings, w);
w->setCentralWidget(view);
w->resize(1024, 768);
w->show();
}
}

View File

@ -1,26 +1,41 @@
#ifndef QCEFVIEWTEST_H
#define QCEFVIEWTEST_H
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QCloseEvent>
#include <QMainWindow>
#include "ui_MainWindow.h"
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include "CefViewWidget.h"
class MainWindow : public QMainWindow
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget* parent = 0);
~MainWindow();
private:
void initBrowser();
private:
QVBoxLayout* m_Layout={};
QHBoxLayout* m_OptionsLayout={};
QHBoxLayout* m_BrowserLayout={};
QPushButton* m_NewBrowser={};
QPushButton* m_ExitAllBrowser={};
QPushButton* m_DevTools={};
QPushButton* m_CallJSCode={};
QPushButton* m_SetFocus={};
QLineEdit* m_TextToUrl={};
QPushButton* m_BtnToUrl={};
QCefView* m_CefWidget ={};
QCefSetting setting;
protected:
void createLeftCefView();
// QCefView slots
protected slots:
void onInvokeMethod(const QCefBrowserId& browserId,
const QCefFrameId& frameId,
const QString& method,
@ -57,12 +72,8 @@ protected slots:
void onBtnCallJSCodeClicked();
void onBtnNewBrowserClicked();
private:
Ui::MainWindow m_ui;
QCefView* m_pLeftCefViewWidget = nullptr;
QCefView* m_pRightCefViewWidget = nullptr;
void onBtnToUrl();
};
#endif // QCEFVIEWTEST_H

View File

@ -1,250 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1280</width>
<height>900</height>
</rect>
</property>
<property name="windowTitle">
<string>QCefViewTest</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>3</number>
</property>
<property name="leftMargin">
<number>2</number>
</property>
<property name="topMargin">
<number>2</number>
</property>
<property name="rightMargin">
<number>2</number>
</property>
<property name="bottomMargin">
<number>2</number>
</property>
<item>
<widget class="QWidget" name="nativeContainer" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">
#nativeContainer {
background-color: rgb(170, 255, 255);
}
</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_nativeContainer">
<property name="spacing">
<number>6</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="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">
#label{
font: 12pt &quot;MS Shell Dlg 2&quot;;
}
</string>
</property>
<property name="text">
<string>Native Area</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_showDevTools">
<property name="text">
<string>Show Left DevTools</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_reloadRight">
<property name="text">
<string>Reload Right QCefView</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_recreateRight">
<property name="text">
<string>Recreate Right QCefView</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_changeColor">
<property name="text">
<string>ChangeColor</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item>
<widget class="QPushButton" name="btn_setFocus">
<property name="text">
<string>SetFocus</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_2"/>
</item>
<item>
<widget class="QPushButton" name="btn_callJSCode">
<property name="text">
<string>CallJSCode</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_3"/>
</item>
<item>
<widget class="QPushButton" name="btn_newBrowser">
<property name="text">
<string>NewBrowser</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_quitApp">
<property name="text">
<string>Exit App</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QSplitter" name="splitter">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QWidget" name="leftCefViewContainer" native="true">
<property name="styleSheet">
<string notr="true">QWidget#leftCefViewContainer {
background-color: rgb(217, 183, 255);
}</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_cefContainer">
<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>
</layout>
</widget>
<widget class="QWidget" name="rightCefViewContainer" native="true">
<property name="styleSheet">
<string notr="true">QWidget#rightCefViewContainer {
background-color: rgb(217, 183, 255);
}</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_cefContainer_2">
<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>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<tabstops>
<tabstop>btn_showDevTools</tabstop>
<tabstop>btn_reloadRight</tabstop>
<tabstop>btn_recreateRight</tabstop>
<tabstop>btn_changeColor</tabstop>
<tabstop>lineEdit</tabstop>
<tabstop>lineEdit_2</tabstop>
<tabstop>btn_callJSCode</tabstop>
<tabstop>lineEdit_3</tabstop>
<tabstop>btn_newBrowser</tabstop>
<tabstop>btn_quitApp</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>