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

}

相关推荐
机器视觉知识推荐、就业指导12 小时前
Qt 6 所有 QML 类型(官方完整清单 · 原始索引版)
开发语言·qt
少控科技13 小时前
QT新手日记033
开发语言·qt
少控科技19 小时前
QT新手日记 030
开发语言·qt
xmRao19 小时前
Qt+FFmpeg 实现 Windows 音频采集
windows·qt·ffmpeg
草莓熊Lotso20 小时前
Qt 控件美化与交互进阶:透明度、光标、字体与 QSS 实战
android·java·开发语言·c++·人工智能·git·qt
GGGLF1 天前
Qt网络/串口通信开发:QByteArray 数据类型转换方法解析
开发语言·qt
掘根1 天前
【jsonRpc项目】基本的宏定义,抽象层和具象层的实现
开发语言·qt
深蓝海拓1 天前
Qt(PySide/PyQt)的信号槽机制的比较深入的学习笔记
qt·学习·pyqt
Henry Zhu1231 天前
Qt Model/View架构详解(一):基础理论
开发语言·qt
青火coding1 天前
ai时代下的RPC传输——StreamObserver
qt·网络协议·microsoft·rpc