QT5事件处理
鼠标事件及实例
鼠标事件包括鼠标的移动,鼠标键按下、松开、单击、双击等。本例将介绍如 何获得和处理鼠标事件。
示例代码:
mouseevent.h
#ifndef TEST_MOUSEEVENT_H #define TEST_MOUSEEVENT_H #include <QMainWindow> #include <QLabel> class MouseEvent : public QMainWindow { Q_OBJECT public: MouseEvent(QWidget *parent = 0); ~MouseEvent() override = default; protected: void mousePressEvent(QMouseEvent *e) override; void mouseMoveEvent(QMouseEvent *e) override; void mouseReleaseEvent(QMouseEvent *e) override; void mouseDoubleClickEvent(QMouseEvent *e) override; private: QLabel *statusLabel; QLabel *MousePosLabel; }; #endif //TEST_MOUSEEVENT_H
mouseevent.cpp
#include "mouseevent.h" #include <QStatusBar> #include <QMouseEvent> MouseEvent::MouseEvent(QWidget *parent) :QMainWindow(parent) { setWindowTitle(tr("鼠标事件")); statusLabel = new QLabel; statusLabel->setText (tr ("当前位置:")); statusLabel->setFixedWidth(100); MousePosLabel = new QLabel; MousePosLabel->setText (tr ("")); MousePosLabel->setFixedWidth(100); statusBar()->addPermanentWidget(statusLabel); statusBar()->addPermanentWidget(MousePosLabel); this->setMouseTracking(true); resize(400,200); } void MouseEvent::mousePressEvent(QMouseEvent *e) { QString str="("+QString::number(e->x())+","+QString::number(e->y()) + ")"; if(e->button()==Qt::LeftButton) statusBar()->showMessage(tr("左键:")+str); else if (e->button () ==Qt::RightButton) statusBar()->showMessage(tr("右键:")+str); else if(e->button()==Qt::MidButton) statusBar()->showMessage(tr("中键:")+str); } void MouseEvent::mouseMoveEvent(QMouseEvent *e) { MousePosLabel->setText("("+QString::number(e->x())+", "+QString::number(e->y())+")"); } void MouseEvent::mouseReleaseEvent(QMouseEvent *e) { QString str="("+QString::number(e->x())+","+QString::number(e->y()) +")"; statusBar()->showMessage (tr ("释放在:") +str, 3000); } void MouseEvent::mouseDoubleClickEvent(QMouseEvent *e) { QWidget::mouseDoubleClickEvent(e); }
main.cpp
#include <QApplication> #include "mouseevent.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MouseEvent ee; ee.show(); return a.exec (); }
示例图片:
键盘事件及实例
在图像处理和游戏应用程序中有时需要通过键盘控制某个对象的移动,此功能 可以通过对键盘事件的处理来实现。键盘事件的获取是通过重定义QWidget类的 keyPressEvent()和 keyReleaseEvent()来实现的。
示例代码:
keyevent.h
#ifndef TEST_KEYEVENT_H #define TEST_KEYEVENT_H #include <QMainWindow> #include <QKeyEvent> #include <QLabel> class KeyEvent : public QMainWindow { Q_OBJECT public: KeyEvent(QWidget *parent = 0); ~KeyEvent() override = default; protected: void keyPressEvent(QKeyEvent *e) override; void keyReleaseEvent(QKeyEvent *e) override; private: QLabel *statusLabel; QLabel *MousePosLabel; }; #endif //TEST_KEYEVENT_H
keyevent.cpp
#include "keyevent.h" #include <QStatusBar> KeyEvent::KeyEvent(QWidget *parent) :QMainWindow(parent) { setWindowTitle(tr("键盘事件")); statusLabel = new QLabel; statusLabel->setText (tr ("当前位置:")); statusLabel->setFixedWidth(100); MousePosLabel = new QLabel; MousePosLabel->setText (tr ("")); MousePosLabel->setFixedWidth(100); statusBar()->addPermanentWidget(statusLabel); statusBar()->addPermanentWidget(MousePosLabel); this->setMouseTracking(true); resize(400,200); } void KeyEvent::keyPressEvent(QKeyEvent *e) { statusBar()->showMessage(tr("按下:")+ toupper(e->key())); } void KeyEvent::keyReleaseEvent(QKeyEvent *e) { statusBar()->showMessage (tr ("释放:") + toupper(e->key()), 3000); }
main.cpp
#include <QApplication> #include "keyevent.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); KeyEvent ee; ee.show(); return a.exec (); }
示例图片:
事件过滤及实例
Qt的事件模型中提供的事件过滤器功能使得一个QObject对象可以监视另一 个QObject对象中的事件,通过在一个QObject对象中安装事件过滤器,可以在事 件到达该对象前捕获事件,从而起到监视该对象事件的效果。
如,Qt已经提供了 QPushButton用于表示一个普通的按钮类,如果需要实现 一个动态的按钮,即当鼠标按下时按钮文字发生变化,则需要同时响应鼠标的按下等事件。
示例代码:
eventfilter.h
#ifndef TEST_EVENTFILTER_H #define TEST_EVENTFILTER_H #include <QMainWindow> #include <QEvent> #include <QLabel> class EventFilter : public QMainWindow { public: explicit EventFilter(QWidget * widget = 0); ~EventFilter() override = default; protected: bool eventFilter(QObject *o,QEvent *e) override; void mousePressEvent(QMouseEvent *e) override ; private: QLabel *label_; }; #endif //TEST_EVENTFILTER_H
eventfilter.cpp
#include "eventfilter.h" #include <QStatusBar> EventFilter::EventFilter(QWidget *widget) { label_ = new QLabel(this); // setCentralWidget(button_); label_->installEventFilter(this); } bool EventFilter::eventFilter(QObject *o, QEvent *e) { if(o==label_ && e->type()==QEvent::MouseButtonPress) { label_->setText("鼠标按下"); } return QMainWindow::eventFilter(o,e); } void EventFilter::mousePressEvent(QMouseEvent *e) { statusBar()->showMessage("鼠标按下"); }
main.cpp
#include <QApplication> #include "EventFilter.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); EventFilter ee; ee.show(); return a.exec (); }
示例图片: