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

}

相关推荐
csdn_zhangchunfeng5 小时前
Qt之slots和Q_SLOTS的区别
开发语言·qt
Larry_Yanan10 小时前
Qt网络开发之基于 QWebEngine 实现简易内嵌浏览器
linux·开发语言·网络·c++·笔记·qt·学习
一然明月12 小时前
Qt QML 锚定(Anchors)全解析
java·数据库·qt
一只爱学习的小鱼儿12 小时前
使用QT编写粒子显示热力图效果
开发语言·qt
大树学长12 小时前
【QT开发】Redis通信相关(一)
redis·qt
笨笨马甲12 小时前
Qt 人脸识别
开发语言·qt
山上三树13 小时前
Qt QObject介绍
开发语言·qt
山上三树13 小时前
QObject、QWidget、Widget三者的关系
qt
坚定学代码14 小时前
qt c++ 局域网聊天小工具
c++·qt·个人开发
笨笨马甲15 小时前
Qt network开发
开发语言·qt