当前位置:网站首页>qt 怎么检测鼠标在不在某个控件上
qt 怎么检测鼠标在不在某个控件上
2022-07-04 12:49:00 【Licht小粉】
方式一:推荐,感觉这种事件过滤器的方法捕捉比较敏感
记得安装事件过滤器
this->installEventFilter(this);
protected:
bool eventFilter(QObject *obj, QEvent *event) override;
//需要过滤的事件
if (event->type() == QEvent::MouseMove)
{
QMouseEvent* mouseEvent = (QMouseEvent*)event;
QPoint p = mouseEvent->pos();
int x = m_LightAddrAndDmxList.value(m_iCurrentTablevie)->x();
int y = m_LightAddrAndDmxList.value(m_iCurrentTablevie)->y();
int w =m_LightAddrAndDmxList.value(m_iCurrentTablevie)->w();
int h = m_LightAddrAndDmxList.value(m_iCurrentTablevie)->h();
if(( x<p.x() && p.x()<w) && ( y<p.y() && p.y()<h))//控件相对于应用程序的坐标
{
return true;
}
//这里做你想做的事
return true;
}
return QWidget::eventFilter(obj,event);
上面的函数还可以有一层,选择对应的你要哪个控件安装这个事件过滤器
比如:
//需要过滤的事件
if(obj == ui->lineEdit)//监控的对象
{
if (event->type() == QEvent::MouseMove)
{
……
return true;
}
}
return QWidget::eventFilter(obj,event);
方式二:重写鼠标事件
void mouseMoveEvent(QMouseEvent *ev);
如果控件有父亲,记得也要设置鼠标追踪
//设置默认追踪鼠标,否则在触发鼠标移动时,必须先点一下才有效
this->setMouseTracking(true);
void MyLabel::mouseMoveEvent(QMouseEvent *ev)
{
//鼠标的位置
int i=ev->x();
int j=ev->y();
}
上面两种方法可以动态的监听鼠标是不是在某个控件上
方式三:静态
if(!ui->lineEdit->geometry().contains(this->mapFromGlobal(QCursor::pos())));
方式四:静态:
if(ui->lineEdit->underMouse)
边栏推荐
- 【Antd踩坑】Antd Form 配合Input.Group时出现Form.Item所占据的高度不对
- Openharmony application development how to create dayu200 previewer
- OPPO Find N2产品形态首曝:补齐各项短板
- [C question set] of VII
- Interview disassembly: how to check the soaring usage of CPU after the system goes online?
- When MDK uses precompiler in header file, ifdef is invalid
- MySQL 45 lecture - learn the actual combat notes of MySQL in Geek time 45 lecture - 06 | global lock and table lock_ Why are there so many obstacles in adding a field to the table
- Qt如何实现打包,实现EXE分享
- 基于链表管理的单片机轮询程序框架
- OpenHarmony应用开发之如何创建DAYU200预览器
猜你喜欢
ViewBinding和DataBinding的理解和区别
Redis - how to install redis and configuration (how to quickly install redis on ubuntu18.04 and centos7.6 Linux systems)
DGraph: 大规模动态图数据集
2022 hoisting machinery command examination simulation 100 questions simulation examination platform operation
嵌入式编程中五个必探的“潜在错误”
Understanding and difference between viewbinding and databinding
30:第三章:开发通行证服务:13:开发【更改/完善用户信息,接口】;(使用***BO类承接参数,并使用了参数校验)
MySQL 45 lecture - learn the actual combat notes of MySQL in Geek time 45 lecture - 06 | global lock and table lock_ Why are there so many obstacles in adding a field to the table
【Antd踩坑】Antd Form 配合Input.Group时出现Form.Item所占据的高度不对
Oracle 被 Ventana Research 评为数字创新奖总冠军
随机推荐
CVPR 2022 | 大幅减少零样本学习所需的人工标注,提出富含视觉信息的类别语义嵌入(源代码下载)...
Qt如何实现打包,实现EXE分享
.Net之延迟队列
Flet教程之 03 FilledButton基础入门(教程含源码)(教程含源码)
嵌入式编程中五个必探的“潜在错误”
C foundation in-depth learning II
2022 hoisting machinery command examination simulation 100 questions simulation examination platform operation
安装trinity、解决报错
30:第三章:开发通行证服务:13:开发【更改/完善用户信息,接口】;(使用***BO类承接参数,并使用了参数校验)
Node の MongoDB 安装
C语言图书租赁管理系统
Introduction to XML I
Commvault 和 Oracle 合作,在 Oracle 云上提供 Metallic数据管理即服务
Scripy framework learning
CommVault cooperates with Oracle to provide metallic data management as a service on Oracle cloud
担心“断气” 德国正修改《能源安全法》
高质量软件架构的唯一核心指标
find命令报错: paths must precede expression(转)
美国土安全部部长警告移民“不要踏上危险的旅程”
分布式BASE理论