日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

(十三)事件分发器——event()函数,事件过滤

發(fā)布時(shí)間:2025/4/16 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (十三)事件分发器——event()函数,事件过滤 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

事件分發(fā)器——event()函數(shù)

事件過(guò)濾

事件進(jìn)入窗口之前被攔截 eventFilter?

#include "mywidget.h" #include "ui_mywidget.h" #include <QDebug>MyWidget::MyWidget(QWidget *parent) :QWidget(parent),ui(new Ui::MyWidget) {ui->setupUi(this);// 給MyLabel 安裝事件過(guò)濾器// 參數(shù) 誰(shuí)來(lái)過(guò)濾label 的事件ui->mylabel->installEventFilter(this);ui->label_2->installEventFilter(this); }MyWidget::~MyWidget() {delete ui; }void MyWidget::mousePressEvent(QMouseEvent *) {qDebug() << "+++++++++++++"; }bool MyWidget::eventFilter(QObject *obj, QEvent *e) {// 判斷對(duì)象if (obj == ui->mylabel) {// 過(guò)濾事件if (e->type() == QEvent::MouseMove){ui->mylabel->setText("++++++++++");return true;}}if (obj == ui->label_2) {if (e->type() == QEvent::MouseMove){ui->label_2->setText("**********");return true;}}// 執(zhí)行默認(rèn)處理return QWidget::eventFilter(obj,e); } mywidget.cpp #include "mylabel.h" #include <QMouseEvent> #include <QTimerEvent> #include <QTimer> #include <QEvent>// QWidget 默認(rèn)是不追蹤鼠標(biāo)事件的 MyLabel::MyLabel(QWidget *parent) : QLabel(parent) {// 設(shè)置窗口追蹤鼠標(biāo)鍵this->setMouseTracking(true);// 啟動(dòng)定時(shí)器// 參數(shù) 1: 觸發(fā)定時(shí)器的時(shí)間, 單位: ms// 參數(shù)2: 使用默認(rèn)值// 返回值: 定時(shí)器IDid = startTimer(2000);id1 = startTimer(3000);// 第二種定時(shí)器用法 // QTimer * timer = new QTimer(this); // timer->start(100); // connect(timer, &QTimer::timeout, this, [=]() // { // static int number = 0; // this->setText(QString::number(number++)); // }); }// 進(jìn)入還是離開(kāi)邊界的一瞬間來(lái)完成的 // 鼠標(biāo)進(jìn)入 void MyLabel::enterEvent(QEvent *) {setText("你不要在我身上亂摸!!!!"); }// 鼠標(biāo)離開(kāi) void MyLabel::leaveEvent(QEvent *) {setText("終于離開(kāi)了..."); }void MyLabel::mousePressEvent(QMouseEvent *ev) {// 字符串拼接 QString().arg()// %1, %2, %3 -- 占位符 QString btn;if(ev->button() == Qt::LeftButton){btn = "LeftButton";}else if(ev->button() == Qt::RightButton){btn = "RightButton";}else if(ev->button() == Qt::MidButton){btn = "MidButton";}QString str = QString("MousePree[%3]:(%1, %2)").arg(ev->x()).arg(ev->y()).arg(btn);setText(str); }void MyLabel::mouseReleaseEvent(QMouseEvent *ev) {QString btn;if(ev->button() == Qt::LeftButton){btn = "LeftButton";}else if(ev->button() == Qt::RightButton){btn = "RightButton";}else if(ev->button() == Qt::MidButton){btn = "MidButton";}QString str = QString("MouseRelease[%3]:(%1, %2)").arg(ev->x()).arg(ev->y()).arg(btn);setText(str); }void MyLabel::mouseMoveEvent(QMouseEvent *ev) {QString btn;if(ev->buttons() & (Qt::LeftButton | Qt::RightButton)){btn = "LeftButton";}else if(ev->buttons() & Qt::RightButton){btn = "RightButton";}else if(ev->buttons() & Qt::MidButton){btn = "MidButton";}QString str = QString("MouseMove[%3]:(%1, %2)").arg(ev->x()).arg(ev->y()).arg(btn);setText(str); }// 每觸發(fā)一次定時(shí)器, 進(jìn)入該函數(shù)中 void MyLabel::timerEvent(QTimerEvent *e) {QString str;if(e->timerId() == id){static int num = -100;str = QString("%1: %2").arg("Time out: ").arg(num++);if(num >= 100){// 關(guān)閉定時(shí)器 killTimer(id);}}else if(e->timerId() == id1){static int num1 = 10000;str = QString("%1: %2").arg("Time out: ").arg(num1++);if(num1 >= 10000+1000){// 關(guān)閉定時(shí)器 killTimer(id1);}}setText(str); }bool MyLabel::event(QEvent *e) {// 返回值// true -- 代表事件被處理了,不再繼續(xù)下發(fā),停止了// false -- 事件沒(méi)有被處理,會(huì)繼續(xù)向下分發(fā)// 大概的處理步驟 // switch(e->type()){ // case QEvent::MouseMove: // mouseMoveEvent(e); // break; // case QEvent::Timer: // timerEvent(e); // break; // }// 過(guò)濾定時(shí)器事件if (e->type() == QEvent::Timer) {return true;// return false,將事件拋給父類 } // else if (e->type() == QEvent::MouseMove) { // return true; // }else if (e->type() == QEvent::MouseButtonPress) {return false;}// 讓父類執(zhí)行默認(rèn)的處理return QLabel::event(e); } mylabel.cpp #ifndef MYWIDGET_H #define MYWIDGET_H#include <QWidget>namespace Ui { class MyWidget; }class MyWidget : public QWidget {Q_OBJECTpublic:explicit MyWidget(QWidget *parent = nullptr);~MyWidget();void mousePressEvent(QMouseEvent *);bool eventFilter(QObject *obj, QEvent *e); private:Ui::MyWidget *ui; };#endif // MYWIDGET_H mywidget.h #ifndef MYLABEL_H #define MYLABEL_H#include <QWidget> #include <QLabel>class MyLabel : public QLabel {Q_OBJECT public:explicit MyLabel(QWidget *parent = nullptr);void enterEvent(QEvent *);void leaveEvent(QEvent *);void mousePressEvent(QMouseEvent *ev);void mouseReleaseEvent(QMouseEvent *ev);void mouseMoveEvent(QMouseEvent *ev);void timerEvent(QTimerEvent *e);bool event(QEvent *e); private:int id;int id1; signals:public slots: };#endif // MYLABEL_H mylabel.h

?

轉(zhuǎn)載于:https://www.cnblogs.com/xiangtingshen/p/10765753.html

總結(jié)

以上是生活随笔為你收集整理的(十三)事件分发器——event()函数,事件过滤的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。