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();

}

相关推荐
luoyayun36119 分钟前
Qt + FFmpeg 音频工具:重采样实现
qt·ffmpeg·重采样
小短腿的代码世界19 小时前
Qt Bluetooth源码深度解析:从HCI协议到跨平台API的完整架构
开发语言·qt·架构
小短腿的代码世界20 小时前
Qt WebEngine多进程架构与IPC通信:从Chromium多进程到Qt信号槽融合
qt·架构·系统架构
尘中远1 天前
【Qwt 7.0 系列】快速入门与核心新特性概览 —— 更现代的 Qt 数据可视化库
qt·数据可视化·绘图·qwt·科学绘图
初阳7851 天前
【Qt】系统相关(3)——多线程
qt
初阳7851 天前
【Qt】系统相关(2)——文件
开发语言·qt
尘中远2 天前
【Qwt 7.0 系列】坐标轴与刻度系统 —— 刻度引擎、网格、图例与刻度朝内
qt·数据可视化·qcustomplot·qwt·工业软件·科学绘图
sycmancia2 天前
Qt——多线程间的互斥
开发语言·qt
尘中远2 天前
【Qwt 7.0 系列】常用图表类型实战 —— 柱状图、散点图、箱线图与直方图
qt·qwt·工业软件·科学绘图
尘中远2 天前
【Qwt 7.0 系列】交互功能详解 —— 平移、缩放、坐标轴交互与数据拾取
qt·数据可视化·绘图·qcustomplot·qwt·科学绘图