当前位置:网站首页>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)
边栏推荐
- Openharmony application development how to create dayu200 previewer
- FS4059C是5V输入升压充电12.6V1.2A给三节锂电池充电芯片 输入小电流不会拉死,温度60°建议1000-1100MA
- C#基础深入学习二
- When MDK uses precompiler in header file, ifdef is invalid
- 免费、好用、强大的轻量级笔记软件评测:Drafts、Apple 备忘录、Flomo、Keep、FlowUs、Agenda、SideNote、Workflowy
- 常见 content-type对应表
- 2022kdd pre lecture | 11 first-class scholars take you to unlock excellent papers in advance
- SCM polling program framework based on linked list management
- 2022 Shandong Province safety officer C certificate examination question bank and online simulation examination
- Getting started with microservices
猜你喜欢
随机推荐
Interview disassembly: how to check the soaring usage of CPU after the system goes online?
Getting started with the go language is simple: go implements the Caesar password
C#基础深入学习一
XML入门一
【R语言数据科学】:交叉验证再回首
2022年起重机械指挥考试模拟100题模拟考试平台操作
【Antd踩坑】Antd Form 配合Input.Group时出现Form.Item所占据的高度不对
程序员的焦虑
JVM系列——栈与堆、方法区day1-2
面试官:Redis中哈希数据类型的内部实现方式是什么?
MongoDB常用28条查询语句(转)
Introduction to XML III
担心“断气” 德国正修改《能源安全法》
Node の MongoDB安装
華昊中天沖刺科創板:年虧2.8億擬募資15億 貝達藥業是股東
一次 Keepalived 高可用的事故,让我重学了一遍它
2022KDD预讲 | 11位一作学者带你提前解锁优秀论文
Doctoral application | West Lake University Learning and reasoning system laboratory recruits postdoctoral / doctoral / research internship, etc
FS4059C是5V输入升压充电12.6V1.2A给三节锂电池充电芯片 输入小电流不会拉死,温度60°建议1000-1100MA
DGraph: 大规模动态图数据集