diff --git a/src/Cef/MainWindow.cpp b/src/Cef/MainWindow.cpp index d5f6bb9..3b05703 100644 --- a/src/Cef/MainWindow.cpp +++ b/src/Cef/MainWindow.cpp @@ -11,64 +11,90 @@ #include -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(); -} - +} \ No newline at end of file diff --git a/src/Cef/MainWindow.h b/src/Cef/MainWindow.h index d4486ff..b424ea5 100644 --- a/src/Cef/MainWindow.h +++ b/src/Cef/MainWindow.h @@ -1,26 +1,41 @@ #ifndef QCEFVIEWTEST_H #define QCEFVIEWTEST_H +#include +#include #include -#include - -#include "ui_MainWindow.h" - +#include +#include +#include #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 diff --git a/src/Cef/MainWindow.ui b/src/Cef/MainWindow.ui deleted file mode 100644 index 8aefc20..0000000 --- a/src/Cef/MainWindow.ui +++ /dev/null @@ -1,250 +0,0 @@ - - - MainWindow - - - - 0 - 0 - 1280 - 900 - - - - QCefViewTest - - - - - 3 - - - 2 - - - 2 - - - 2 - - - 2 - - - - - - 0 - 0 - - - - - #nativeContainer { - background-color: rgb(170, 255, 255); - } - - - - - 6 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - 0 - 0 - - - - - #label{ - font: 12pt "MS Shell Dlg 2"; - } - - - - Native Area - - - Qt::AlignCenter - - - - - - - Show Left DevTools - - - - - - - Reload Right QCefView - - - - - - - Recreate Right QCefView - - - - - - - ChangeColor - - - - - - - - - - SetFocus - - - - - - - - - - CallJSCode - - - - - - - - - - NewBrowser - - - - - - - Exit App - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - - - - - 0 - 0 - - - - QFrame::Plain - - - Qt::Horizontal - - - - QWidget#leftCefViewContainer { - background-color: rgb(217, 183, 255); -} - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - QWidget#rightCefViewContainer { - background-color: rgb(217, 183, 255); -} - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - - - - - - btn_showDevTools - btn_reloadRight - btn_recreateRight - btn_changeColor - lineEdit - lineEdit_2 - btn_callJSCode - lineEdit_3 - btn_newBrowser - btn_quitApp - - - -