QT - 事件过滤

#include <QApplication>

#include <QMainWindow>

#include <QEvent>

#include <QDebug>

class MainWindow : public QMainWindow {

// ...

};

class ModalEventFilter : public QObject {

bool eventFilter(QObject *watched, QEvent *event) override {

if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease) {

// 如果弹窗打开,则过滤掉鼠标事件

if (isModal) {

return true; // 过滤掉事件

}

}

return QObject::eventFilter(watched, event);

}

void setModal(bool modal) {

isModal = modal;

}

private:

bool isModal = false;

};

int main(int argc, char *argv\[\]) {

QApplication a(argc, argv);

MainWindow w;

ModalEventFilter eventFilter;

w.installEventFilter(&eventFilter); // 为主窗口安装事件过滤器

// 弹窗打开时

eventFilter.setModal(true);

// 弹窗关闭时

// eventFilter.setModal(false);

w.show();

return a.exec();

}

相关推荐
hy.z_7771 小时前
【QT】1.QT初识(背景介绍、搭建开发环境、Qt Creator、程序、项目文件解析、编程注意事项)
qt
不会代码的小猴2 小时前
7. JSON
开发语言·c++·笔记·qt·算法·json
沫璃染墨3 小时前
《Qt从零入门系列(六):信号与槽进阶——从多种连接方式到Lambda表达式》
开发语言·c++·qt·代码规范·设计规范·qt5
橙橙笔记6 小时前
QT的安装
开发语言·qt·安装·软件
AI的探索之旅7 小时前
图像格式与转换一文讲透:相机 / OpenCV / Qt
数码相机·qt·opencv
2kπ8 小时前
QT开发笔记
开发语言·笔记·qt
Quz10 小时前
QML MouseArea:点击、悬停与坐标获取
qt
程与留11 小时前
04_Qt项目构建流程深度解析——MOC、UIC、RCC 的作用与原理
qt
Quz11 小时前
QML MouseArea 事件:单击双击、长按与多按键检测
qt
秋田君12 小时前
SqlServer2022 服务以及客户端可视化安装详细教程
qt