当前位置:网站首页>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)
边栏推荐
- 1200. Minimum absolute difference
- Flet tutorial 03 basic introduction to filledbutton (tutorial includes source code) (tutorial includes source code)
- Distributed base theory
- Scripy framework learning
- unity不识别rider的其中一种解决方法
- DGraph: 大规模动态图数据集
- Fs7867s is a voltage detection chip used for power supply voltage monitoring of digital system
- .Net之延迟队列
- 如何在 2022 年为 Web 应用程序选择技术堆栈
- 基于链表管理的单片机轮询程序框架
猜你喜欢
Dgraph: large scale dynamic graph dataset
Distributed base theory
【R语言数据科学】:交叉验证再回首
Redis - how to install redis and configuration (how to quickly install redis on ubuntu18.04 and centos7.6 Linux systems)
JVM series - stack and heap, method area day1-2
Flet教程之 03 FilledButton基础入门(教程含源码)(教程含源码)
如何在 2022 年为 Web 应用程序选择技术堆栈
Three schemes to improve the efficiency of MySQL deep paging query
分布式BASE理论
Understanding and difference between viewbinding and databinding
随机推荐
.Net之延迟队列
[C question set] of VII
1200. 最小绝对差
Scrapy 框架学习
分布式BASE理论
Distributed base theory
Summary of recent days (non-technical article)
CommVault cooperates with Oracle to provide metallic data management as a service on Oracle cloud
Database lock table? Don't panic, this article teaches you how to solve it
源码编译安装MySQL
Gorm 读写分离(转)
Variable promotion and function promotion in JS
自主工业软件的创新与发展
基于STM32+华为云IOT设计的酒驾监控系统
Ws2811 m is a special circuit for three channel LED drive and control, and the development of color light strip scheme
Introduction to XML I
Automatic filling of database public fields
E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
js中的变量提升和函数提升
Node の MongoDB 安装