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

}

相关推荐
秋田君6 小时前
QT_QFontDialog类字体对话框
开发语言·qt
mabing99315 小时前
Qt QMessageBox、QDialogButtonBox中英文翻译动态切换
开发语言·qt
秋田君15 小时前
QT_QColorDialog颜色对话框
java·数据库·qt
秋田君16 小时前
QT_QT布局常用类QSplitter窗口分割类与QDockWidget窗口停靠类
开发语言·数据库·qt
Lhan.zzZ17 小时前
深入理解 windeployqt:混合 C++/Qt 项目的打包指南
开发语言·c++·qt
Lhan.zzZ18 小时前
QML 开发中的“陷阱”:动态模型更新时,ComboBox 索引为何总是失效?
开发语言·qt
咯哦哦哦哦18 小时前
qt creator x86交叉编译arm 配置
开发语言·qt
秋田君20 小时前
QT_QT布局常用类QStackedWidget与QLayout类
开发语言·qt
用户8402409736081 天前
使用 PyQt5 打造功能完备的 PDF 编辑器:从设计到实现全解析
qt
Quz2 天前
QML RangeSlider 组件:范围选择与双滑块样式
qt