-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathSampleWnd.cpp
84 lines (70 loc) · 2.92 KB
/
SampleWnd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "SampleWnd.h"
#include "QWebView/Creator.h"
#include "QWebView/Manager.h"
#include <QTimer>
SampleWnd::SampleWnd(QWebView::BrowserEngine engine, QWidget* parent /*= nullptr*/) :
QWidget(parent) {
if (engine == QWebView::BrowserEngine::WebView2) {
webview_ = CreateWebView2();
setWindowTitle(windowTitle() + "当前是 WebView2 内核");
}
else if (engine == QWebView::BrowserEngine::CEF) {
webview_ = CreateCEF();
setWindowTitle(windowTitle() + "当前是 CEF 内核");
}
webview_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(webview_, &QWebView::messageReceived, this, [this](QString message) {
QMessageBox::information(this, "Message from JavaSript", message, QMessageBox::Ok);
});
QPushButton* btnClose = new QPushButton("调用QWidget::close()");
connect(btnClose, &QPushButton::clicked, this, [this]() {
this->close();
});
QPushButton* btnCloseLeft = new QPushButton("调用QWebView::close()");
connect(btnCloseLeft, &QPushButton::clicked, this, [this]() {
webview_->close();
});
QPushButton* btnQuit = new QPushButton("调用QCoreApplication::quit()");
connect(btnQuit, &QPushButton::clicked, this, [this]() {
QMessageBox::warning(this, "使用错误", "不能直接调用 QCoreApplication::quit() 来退出应用程序!");
});
QHBoxLayout* lTop = new QHBoxLayout();
lTop->addWidget(webview_);
QHBoxLayout* lBottom = new QHBoxLayout();
lBottom->addWidget(btnCloseLeft);
lBottom->addWidget(btnClose);
lBottom->addWidget(btnQuit);
QVBoxLayout* v = new QVBoxLayout();
v->addLayout(lTop);
v->addLayout(lBottom);
setLayout(v);
webview_->navigate(QString("file:///%1").arg(QCoreApplication::applicationDirPath() + u8"/asserts/test.html"));
QTimer* timer = new QTimer(this);
timer->setInterval(1500);
connect(timer, &QTimer::timeout, this, [this]() {
webview_->postMessage("hi, this is C++ message.");
});
timer->start();
}
void SampleWnd::closeEvent(QCloseEvent* e) {
QWebViewManager::TopLevelWndCloseState state = QWebViewManager::Get()->topLevelWinCloseState(this);
qDebug() << ">>>> SampleWnd closeEvent" << state;
if (state == QWebViewManager::TopLevelWndCloseState::NotStart) {
if (QMessageBox::Yes == QMessageBox::question(this, "警告", "确定要退出示例程序吗?")) {
QWebViewManager::Get()->prepareToCloseTopLevelWindow(this);
}
qDebug() << ">>>> SampleWnd closeEvent: ignore";
e->ignore();
}
else if (state == QWebViewManager::TopLevelWndCloseState::BrowsersClosing) {
qDebug() << ">>>> SampleWnd closeEvent: ignore";
e->ignore();
}
else if (state == QWebViewManager::TopLevelWndCloseState::BrowsersClosed) {
qDebug() << ">>>> SampleWnd closeEvent: accept";
e->accept();
}
else if (state == QWebViewManager::TopLevelWndCloseState::Closed) {
Q_UNREACHABLE();
}
}