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

}

相关推荐
郝学胜_神的一滴7 小时前
Qt 高级开发 031:QListWidget图标布局实战
c++·qt
Vertira8 小时前
如何对QT开发的软件进行打包[已解决]
开发语言·qt
大智兄9 小时前
128.配置qt(交叉)编译的路径---解决无法编译的问题
qt
Henry Zhu12314 小时前
Qt 元对象系统源码级理解
qt
读书札记202214 小时前
Qt中windeployqt.exe工具的使用:解决使用CMake创建的项目点击exe文件后系统提示0xc000007b的问题
开发语言·qt
luoyayun36115 小时前
Qt + FFmpeg 实战:实现音频格式转换功能
qt·ffmpeg·音频格式转换
Henry Zhu12316 小时前
Qt 信号槽、事件循环与线程通信源码级理解
开发语言·qt
郝学胜-神的一滴16 小时前
CMake 015:日志级别全解析
linux·开发语言·c++·qt·程序人生·软件构建·cmake
数据法师1 天前
QuickSay :基于 Qt 的轻量级快捷短语管理工具
开发语言·qt
小短腿的代码世界1 天前
行情快照与增量更新引擎:Qt在高频交易数据分发中的核心架构——你的行情推送为什么延迟了500ms?
开发语言·qt·架构