19.QT-事件發送函數sendEvent()、postEvent()
阿新 • • 發佈:2018-05-15
oid dynamic ... gevent keyevent 等待 事件對象 geometry 鍵盤
Qt發送事件分為兩種
-阻塞型事件發送
- 需要重寫接收對象的event()事件處理函數
- 當事件發送後,將會立即進入event()事件處理函數進行事件處理
- 通過sendEvent()靜態函數實現阻塞發送:
bool QApplication::sendEvent ( QObject * receiver, QEvent * event ) ; // receiver:接收對象, event :要發送的event類型(比如:鼠標雙擊) //當有事件發送,將會調用receiver對象裏的event()成員函數進行事件處理
- sendEvent()函數
- new分配的事件對象被處理後,會由Qt內部自動摧毀
-非阻塞型事件發送
- 需要重寫接收對象的event()事件處理函數
- 當事件發送後立即返回,事件將會發送到事件隊列中等待處理
- 通過postEvent()靜態函數實現非阻塞發送:
void QApplication::postEvent ( QObject * receiver, QEvent * event );
- postEvent()函數是非阻塞式的,所以只能支持棧堆空間事件對象的發送(new分配的對象)
- new分配的事件對象被處理後,會由Qt內部自動摧毀
示例-通過sendEvent()發送預定義的鍵盤Delete按鍵事件
Wiget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QtGui> class Widget : public QWidget { Q_OBJECT private : QTextEdit edit; QPushButton bt; private slots: void onPushButton();public: explicit Widget(); }; #endif // WIDGET_H
Widget.c
#include "widget.h" Widget::Widget() : edit(this),bt("Delete",this) { edit.setGeometry(0,0,300,200); bt.setGeometry(310,20,48,32); connect(&bt, SIGNAL(clicked()), this, SLOT(onPushButton())); } void Widget::onPushButton() { int key = Qt::Key_Delete ; QKeyEvent EventPress(QEvent::KeyPress,key,Qt::NoModifier); QApplication::sendEvent(&edit,&EventPress); //發送鍵盤按下事件 QKeyEvent EventRelease(QEvent::KeyRelease,key,Qt::NoModifier); QApplication::sendEvent(&edit,&EventRelease); //發送鍵盤松開事件 }
效果
自定義事件對象
- 自定義的事件類必須繼承自QEvent,並且無需指定父類
- 自定義的事件類必須擁有全局唯一的Type值,使用QEvent::User+value即可
- 自定義事件類示例:
class StringEvent : public QEvent { public: static const Type = static_cast<Type>(QEvent::User+0x10); explicit QStringEvent(); //... ... } QStringEvent::QStringEvent() : QEvent(TYPE) //註冊TYPE值 { //... ... }
- 由於Qt不認識自定義事件,所以程序裏需要提供處理自定義事件對象的方法
常用兩種方法來處理
1.通過事件過濾器處理
- 通過installEventFilter()安裝到目標對象
- 在eventFilter()函數裏處理事件
2.重寫目標對象的event()函數
- 在event ()函數裏處理事件
示例-通過單擊鼠標按鈕發送自定義事件QStringEvent
QStringEvent文件:
class QStringEvent : public QEvent { private: QString mstr; public: const static Type TYPE = static_cast<Type>(QEvent::User+0x10); explicit QStringEvent(QString s); QString str(); }; QStringEvent::QStringEvent(QString s) : QEvent(TYPE) { mstr = s; } QString QStringEvent::str() { return mstr; }
widget文件:
class Widget : public QWidget { private : QLabel label; bool event( QEvent * e); bool eventFilter(QObject *obj, QEvent *event); public: explicit Widget(); };
Widget::Widget() : label(this) { label.installEventFilter(this); } bool Widget::event( QEvent * e) { if(e->type() == QEvent::MouseButtonPress) { QMouseEvent *me = dynamic_cast<QMouseEvent* >(e);if(me->button() == Qt::LeftButton) { QStringEvent event( QString("X坐標: %1 Y坐標: %2").arg(me->x()).arg(me->y()) ); QApplication::sendEvent(&label,&event); //發送自定義事件 } } return QWidget::event(e); } bool Widget::eventFilter(QObject *obj, QEvent *event) { QLabel *t = dynamic_cast<QLabel* >(obj); if(t && event->type()== QStringEvent::TYPE ) { QStringEvent *str = dynamic_cast<QStringEvent* >(event); t->setText(str->str()); t->adjustSize(); return true; } return QWidget::eventFilter(obj,event); }
效果:
19.QT-事件發送函數sendEvent()、postEvent()