-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathMainWindow.cpp
48 lines (42 loc) · 1.25 KB
/
MainWindow.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
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "CustomEvent.h"
#include <QCoreApplication>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow{parent}
, ui{new Ui::MainWindow}
{
ui->setupUi(this);
connect(ui->btnSend, &QPushButton::clicked, [this](){
qDebug() << "before send event";
CustomEvent custom{"by send"};
// send event 需要自己释放
QCoreApplication::sendEvent(this, &custom);
qDebug() << "after send event";
});
connect(ui->btnPost, &QPushButton::clicked, [this](){
qDebug() << "before post event";
// post event 交给队列管理
QCoreApplication::postEvent(this, new CustomEvent{"by post"});
qDebug() << "after post event";
});
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::event(QEvent *evt)
{
if (evt->type() == CustomEvent::IsCustom)
{
CustomEvent *custom = static_cast<CustomEvent *>(evt);
if (custom) {
// 设置 accept,避免被当作未处理的继续往 parent 传递
custom->accept();
qDebug() << __FUNCTION__ << custom->message() << custom->type();
return true;
}
}
return QMainWindow::event(evt);
}