鼠标悬停、事件触发试用,及信号发送

鼠标悬停、事件触发试用,及信号发送 

                                               ——该笔记省略了很多实现代码,诸如图片加载等 仅作标题中的几个功能实现

 

实现功能:

试用Qlabel类,加载一幅图片后,重载QLabel类的成员函数mouseMoveEvent, 触发事件为显示

在ImageLabel鼠标位置的坐标值,还用到了信号槽

 

具体实现方法及代码:

1. ImageLabel.h

[cpp]  view plain copy
  1. #ifndef IMAGELABEL_H  
  2. #define IMAGELABEL_H  
  3.   
  4. #include <QLabel>  
  5. #include <QDebug>  
  6. #include <QMouseEvent>  
  7.   
  8. class ImageLabel : public QLabel  
  9. {  
  10.     Q_OBJECT  
  11. public:  
  12.     explicit ImageLabel(QWidget *parent = 0);  
  13.     void mouseMoveEvent(QMouseEvent *ev);  
  14.     void enterEvent(QEvent *);  
  15. signals:  
  16.     void customMouseMoveEvent(int x,int y);  
  17. };  
  18.   
  19. #endif // IMAGELABEL_H  

2. ImageLabel.cpp

[cpp]  view plain copy
  1. #include "imagelabel.h"  
  2.   
  3. ImageLabel::ImageLabel(QWidget *parent) :  
  4.   
  5.     QLabel(parent)  
  6.   
  7. {  
  8. }  
  9.   
  10. void ImageLabel::enterEvent(QEvent *) //随着鼠标移动会及时更新鼠标位置  
  11. {  
  12.     setMouseTracking(true);  
  13. }  
  14.   
  15. void ImageLabel::mouseMoveEvent(QMouseEvent *ev){  
  16.   
  17.     customMouseMoveEvent(ev->x(),ev->y());  
  18. }  

槽函数

[cpp]  view plain copy
  1. private slots :  
  2.     void setMousePosition(int,int);  

槽函数实现:

[cpp]  view plain copy
  1. void CLASSX::setMousePosition(int x, int y){  
  2.     qDebug()<<x<<" "<<y;  
  3. }  

把信号和槽连接

[cpp]  view plain copy
  1. ImageLabel* imageLabel = new ImageLabel();  
  2. connect(imageLabel,SIGNAL(customMouseMoveEvent(int,int)) ,  
  3.                            this,SLOT(setMousePosition(int,int)));  

描述:

在一个继承了QLabel类中重载了鼠标移动事件mouseMoveEvent(QMouseEvent*ev) ,使得鼠标每次移动都有以上信号槽,从customMouseMoveEvent(int,int)将信号发送到this类中的slots槽函数setMousePostion(int,int),槽函数对应的两个参数负责接收SIGNAL中的信号量里的两个参数改变的值

3.测试结果

随着鼠标在图片上移动出现了坐标的变化

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值