3.QT事件处理,消息过滤器
1? 新建一個項目:06Event
新建cpp文件
| 06Event.pro |
| HEADERS += \ ??? MyWidget.h ? SOURCES += \ ??? MyWidget.cpp QT += widgets gui |
| MyWidget.h |
| #ifndef MYWIDGET_H #define MYWIDGET_H ? #include <QWidget> #include <QPushButton> #include <QLineEdit> class MyWidget : public QWidget { ??? Q_OBJECT public: ??? explicit MyWidget(QWidget *parent = 0); ??? bool event(QEvent *); ? ??? void mousePressEvent(QMouseEvent *); ??? void mouseReleaseEvent(QMouseEvent *); ??? void mouseMoveEvent(QMouseEvent *); ? ??? /*不要用,因為它默認調用兩次mousePressEvent*/ ??? //void mouseDoubleClickEvent(QMouseEvent *); ? ??? void keyPressEvent(QKeyEvent *); ??? void keyReleaseEvent(QKeyEvent *); ? ??? void closeEvent(QCloseEvent *); ??? //void showEvent(QShowEvent *); ??? //void hideEvent(QHideEvent *); ??? void paintEvent(QPaintEvent *); ? ??? QPushButton* button; ??? QLineEdit* edit; ? signals: ? public slots: ??? void slotButtonClicked(); }; ? #endif // MYWIDGET_H |
| MyWidget.cpp |
| #include "MyWidget.h" #include <QApplication> #include <QEvent> #include <QDebug> #include <QMouseEvent> #include <QKeyEvent> #include <QVBoxLayout> #include <QPainter> ? MyWidget::MyWidget(QWidget *parent) : ??? QWidget(parent) { //? QVBoxLayout* lay = new QVBoxLayout(this); #if 0 ??? QPushButton* button2; ??? lay->addWidget(button = new QPushButton("OK", this)); ??? lay->addWidget(button2 = new QPushButton("Button2")); ??? //設置它為默認的焦點,當點擊tab鍵之后可以切換焦點 ??? button->setDefault(true); ? ??? // 鼠標不需要按下,mouseMove就能得到調用 ??? this->setMouseTracking(true); ? ??? connect(button2, SIGNAL(clicked()), this, SLOT(slotButtonClicked())); ??? connect(button, SIGNAL(clicked()), this, SLOT(slotButtonClicked())); #endif //??? lay->addWidget(edit = new QLineEdit()); //??? connect(edit, SIGNAL(returnPressed()), this, SLOT(slotButtonClicked())); } ? //通過這一句實現點擊按鈕的時候獲得按鈕上的text()內容 void MyWidget::slotButtonClicked() { ??? QLineEdit* button = (QLineEdit*)sender(); ??? qDebug() << button->text(); } ? /* QApplication先得到->具體應該處理的窗口::event()->event()根據消息類型來調用具體的虛函數 */ /* 1)可以重載具體的虛函數,來實現對消息的響應 ?? 2)可以重載event函數,用來處理或者截取消息 */ ? /* 截取消息 */ bool MyWidget::event(QEvent *ev) { ??? // 鼠標消息被截斷 ?? // if(ev->type() == QEvent::MouseButtonPress) ???? //?? return true; ??? ev->accept(); ? ??? return QWidget::event(ev); } ? /** * @brief MyWidget::closeEvent 窗口關閉的事件 */ void MyWidget::closeEvent(QCloseEvent *) { ??? qDebug() << "closeEvent"; } ? void MyWidget::paintEvent(QPaintEvent *) { ??? QPainter p(this); ??? //通過下面的方式實現畫線 ??? p.drawLine(QPoint(0, 0), QPoint(100, 100)); } ? /** * @brief MyWidget::mousePressEvent 鼠標按下的事件 * @param ev */ void MyWidget::mousePressEvent(QMouseEvent *ev) { #if 0 ??? QPoint pt = ev->pos(); ??? qDebug() << pt; ??? //如果鼠標按下的是左鍵的處理 ??? if(ev->button() == Qt::LeftButton){} ? ??? //如果按下的Shift鍵了 ??? if(ev->modifiers() == Qt::ShiftModifier) ??? { ??????? qDebug() << "shift press"; ??? } #endif ??? //改進辦法:先判斷是否有左鍵,然后如果判斷是否還按了Ctrl鍵 ??? if(ev->button() == Qt::LeftButton) ??? { ??????? if(ev->modifiers() == Qt::ControlModifier) ??????? { ??????????? // handle with Control; ??????????? return; ??????? } ??????? // handle2 without control; ??? } else {} } ? /** * @brief MyWidget::mouseReleaseEvent 鼠標釋放的按鍵 */ void MyWidget::mouseReleaseEvent(QMouseEvent *){} ? /** * @brief MyWidget::mouseMoveEvent 鼠標移動的事件 */ void MyWidget::mouseMoveEvent(QMouseEvent *) { ??? static int i=0; ??? qDebug() << "mouse move"<< i++; } ? /** * @brief MyWidget::keyPressEvent 鼠標按下的事件,通過這個可以獲得按下的鍵 * @param ev */ void MyWidget::keyPressEvent(QKeyEvent *ev) { ??? ev->modifiers(); ??? int key = ev->key(); ??? qDebug() << key; ??? char a = key; ??? qDebug() << (char)a; } ? /** * @brief MyWidget::keyReleaseEvent 按鍵釋放的事件 */ void MyWidget::keyReleaseEvent(QKeyEvent *){} ? int main(int argc, char* argv[]) { ??? QApplication app(argc, argv); ? ??? MyWidget w; ??? w.show(); ? ??? return app.exec(); } |
| 運行結果: |
2? QT消息過濾器
| EventFilter.pro |
| HEADERS += \ ??? MyWidget.h \ ??? MyApplication.h ? SOURCES += \ ??? MyWidget.cpp \ ??? MyApplication.cpp ? QT += widgets gui |
| MyWidget.h |
| #ifndef MYWIDGET_H #define MYWIDGET_H ? #include <QWidget> #include <QPushButton> class MyWidget : public QWidget { ??? Q_OBJECT public: ??? explicit MyWidget(QWidget *parent = 0); ? ??? QPushButton* _button; ??? bool eventFilter(QObject *, QEvent *); ??? bool event(QEvent *); signals: ? public slots: ? }; ? #endif // MYWIDGET_H |
| MyWidget.cpp |
| #include "MyWidget.h" #include <QPushButton> #include <QEvent> #include "MyApplication.h" #include <QDebug> #include <QApplication> ? MyWidget::MyWidget(QWidget *parent) : ??? QWidget(parent) { ??? QPushButton* button; ? ??? button = new QPushButton("This button", this); ??? connect(button, SIGNAL(clicked()), this, SLOT(close())); ??? _button = button; ? ??? /*button給自己安裝了一個消息過濾器,那么經過button的消息, ???? * 都先要調用它的過濾器的eventFilter函數*/ ??? button->installEventFilter(this); } ? bool MyWidget::eventFilter(QObject *o, QEvent *e) { #if 0 ??? if(0 == (QObject*)_button &&( ??????????????? e->type() == QEvent::MouseButtonRelease || ??????????????? e->type() == QEvent::MouseButtonDblClick || ??????????????? e->type() == QEvent::MouseButtonPress)) { ??????? return true; ??? } #endif ? ??? return QWidget::eventFilter(o,e); } ? bool MyWidget::event(QEvent *e) { ??? if(e->type() == QEvent::User) ??? { ??????? qDebug() << "User event is comming"; ??? } ??? return QWidget::event(e); } ? int main(int argc,char *argv[]) { ??? MyApplication app(argc,argv); ??? MyWidget w; ??? w.show(); ? ??? //發送一個Event給MyWidget ??? qDebug() << "begin send"; ??? app.postEvent(&w,new QEvent(QEvent::User)); ??? qDebug() << "end send"; ? ??? return app.exec(); } |
| MyApplication.h |
| #ifndef MYAPPLICATION_H #define MYAPPLICATION_H ? #include <QApplication> ? class MyApplication : public QApplication { ??? Q_OBJECT public: ??? MyApplication(int argc,char *argv[]):QApplication(argc,argv) ??? {} ??? bool notify(QObject *,QEvent *); ? signals: ? public slots: ? }; ? #endif // MYAPPLICATION_H |
| MyApplication.cpp |
| #include "MyApplication.h" #include <QEvent> ? #include <QDebug> bool MyApplication::notify(QObject *o, QEvent *e) { ??? if(this->topLevelWidgets().count()>0) ??? { ??????? QWidget* mainWnd = this->topLevelWidgets().at(0); ??????? if(o==(QObject*)mainWnd && e->type() == QEvent::MouseButtonPress) ??????? { ??????????? // do ... ??????????? qDebug() << "mainwnd is clicked"; ??????? } ??? } ? ??? return QApplication::notify(o, e); } |
| 運行結果:
|
?
總結
以上是生活随笔為你收集整理的3.QT事件处理,消息过滤器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 企业账户怎么才能转账到私人账户
- 下一篇: 买了车险可以退吗 可以电话咨询保险公司