Qt 自定义事件代码练习
1、定义自定义事件,继承自QEvent类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #ifndef COUSTOMEVENT_H 2 #define COUSTOMEVENT_H 3 4 #include <QEvent> 5 #include<QObject> 6 7 class CoustomEvent : public QEvent 8 { 9 public: 10 explicit CoustomEvent(); 11 static Type eventType(); 12 QString Str(); 13 14 private: 15 static QEvent::Type m_EventType; // 静态私有变量 16 QString str; 17 }; 18 19 #endif // COUSTOMEVENT_H
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "coustomevent.h" 2 3 QEvent::Type CoustomEvent::m_EventType = QEvent::None; 4 5 CoustomEvent::CoustomEvent() 6 : QEvent(eventType()) 7 { 8 } 9 10 QEvent::Type CoustomEvent::eventType() 11 { 12 // 创建并返回QEvent::Type枚举 13 if(m_EventType == QEvent::None) 14 m_EventType =(QEvent::Type)QEvent::registerEventType(23333); 15 16 return m_EventType; 17 } 18 19 QString CoustomEvent::Str() 20 { 21 22 return str = QString("I am test CoustomEvent"); 23 }
2、ui界面拖一个TextEdit以及按钮,根据两个控件信号触发自定义事件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 6 QT_BEGIN_NAMESPACE 7 namespace Ui { class MainWindow; } 8 QT_END_NAMESPACE 9 10 /** @details 每次改变textEdit内部文字, 11 * 触发textChanged()信号, 12 * 再添加自定义事件进队列,有优先级。 13 * 14 * 每次触发按钮clicked信号, 15 * 立刻发送自定义事件给接收者。 16 */ 17 class CoustomEvent; 18 19 class MainWindow : public QMainWindow 20 { 21 Q_OBJECT 22 23 public: 24 MainWindow(QWidget *parent = nullptr); 25 ~MainWindow(); 26 27 private slots: 28 void on_textEdit_textChanged(); 29 30 void on_pushButton_clicked(); 31 32 private: 33 Ui::MainWindow *ui; 34 CoustomEvent *e; 35 36 private: 37 void customEvent(QEvent *event) override; // 接受自定义事件专用 38 bool event(QEvent *event) override; 39 }; 40 #endif // MAINWINDOW_H
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 #include "coustomevent.h" 4 #include<QDebug> 5 #include <QEvent> 6 7 MainWindow::MainWindow(QWidget *parent) 8 : QMainWindow(parent) 9 , ui(new Ui::MainWindow) 10 { 11 ui->setupUi(this); 12 } 13 14 MainWindow::~MainWindow() 15 { 16 delete ui; 17 } 18 19 20 void MainWindow::on_textEdit_textChanged() 21 { 22 // QCoreApplication::postEvent添加的事件必须堆上建立 23 e = new CoustomEvent(); 24 qDebug()<<e->type(); 25 qApp->postEvent(this, e); // 事件添加至队列,第三个值可以设置优限度,post后会自动delete 26 } 27 28 bool MainWindow::event(QEvent *event) 29 { 30 if(event->type() == CoustomEvent::eventType()) 31 { 32 QString str = static_cast<CoustomEvent*>(event)->Str(); 33 qDebug()<<str; 34 qDebug()<<"I am event!!!!!"; 35 // return true; // 正常情况是不注释,这里是为了演示 36 } 37 38 return QWidget::event(event); 39 } 40 41 void MainWindow::customEvent(QEvent *event) 42 { 43 if(event->type() == CoustomEvent::eventType()) 44 { 45 QString str = static_cast<CoustomEvent*>(event)->Str(); 46 qDebug()<<str; 47 qDebug()<<"I am customEvent!!!!!"<<"\n"; 48 } 49 } 50 51 52 void MainWindow::on_pushButton_clicked() 53 { 54 e = new CoustomEvent(); 55 qDebug()<<e->type(); 56 qApp->sendEvent(this, e); // 事件立刻发送给接收者,send完后要记得delete 57 delete e; 58 e = nullptr; 59 }
3、Qt事件机制运行图(转自:https://blog.csdn.net/michealtx/article/details/6865891)