当前位置:网站首页>Qt | 关于如何使用事件过滤器 eventFilter
Qt | 关于如何使用事件过滤器 eventFilter
2022-07-26 09:06:00 【InfoQ】
前言:
使用方法:
- bool eventFilter(QObject *obj, QEvent *event);
bool Widget::eventFilter(QObject *obj, QEvent event)
{
if(obj == ui->textEdit)
{
if(event->type == QEvent::Wheel){
return true;
}
else{
return false;
}
}
else{
return QWidget::eventFilter(obj, event);
}
}
为什么要使用过滤器:
发送事件:
- bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event);
- void QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority = Qt::NormalEventPriority);
sendEvent和postEvent的区别:
- sendEvent()会立即处理给定的事件;而postEvent()则会将事件放到等待调度队列中,当下一次Qt的主事件循环运行时才会处理它。
- sendEvent()中的QEvent队形参数在事件发送完成后无法自动删除,所以需要在栈上创建QEvent对象;而postEvent()中的QEvent对象参数必须在堆上进行创建(例如使用new),当事件被发送后事件队列会自动删除它。
QKeyEvent myEvent(QEvent::KeyPress, Qt::Key_Up, Qt::NoModify);
qApp->sendEvent(ui->spinBox, &myEvent);//发送键盘事件到spinbox部件
边栏推荐
猜你喜欢
随机推荐
Mutual transformation of array structure and tree structure
Nuxt - Project packaging deployment and online to server process (SSR server rendering)
Database operation topic 1
数据库操作 题目二
(2006,Mysql Server has gone away)问题处理
unity简易消息机制
Canal 的学习笔记
CF1481C Fence Painting
“could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32” 问题处理
redis原理和使用-基本特性
Zipkin安装和使用
2022年上海市安全员C证考试试题及模拟考试
ext4文件系统打开了DIR_NLINK特性后,link_count超过65000的后使用link_count=1来表示数量不可知
Learn more about the difference between B-tree and b+tree
JDBC database connection pool (Druid Technology)
对象型的集合按某个属性的值进行去重
Uploading pictures on Alibaba cloud OSS
redis原理和使用-安装和分布式配置
What is the difference between NFT and digital collections?
围棋智能机器人阿法狗,阿尔法狗机器人围棋









