您的位置:首页 > 其它

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

2013-10-23 20:34 621 查看


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

分类: Qt2012-05-18
16:45 692人阅读 评论(0) 收藏 举报

signal测试

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

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

 
实现功能:
试用Qlabel类,加载一幅图片后,重载QLabel类的成员函数mouseMoveEvent, 触发事件为显示
在ImageLabel鼠标位置的坐标值,还用到了信号槽
 
具体实现方法及代码:
1. ImageLabel.h

[cpp] view
plaincopy

#ifndef IMAGELABEL_H  

#define IMAGELABEL_H  

  

#include <QLabel>  

#include <QDebug>  

#include <QMouseEvent>  

  

class ImageLabel : public QLabel  

{  

    Q_OBJECT  

public:  

    explicit ImageLabel(QWidget *parent = 0);  

    void mouseMoveEvent(QMouseEvent *ev);  

    void enterEvent(QEvent *);  

signals:  

    void customMouseMoveEvent(int x,int y);  

};  

  

#endif // IMAGELABEL_H  

2. ImageLabel.cpp

[cpp] view
plaincopy

#include "imagelabel.h"  

  

ImageLabel::ImageLabel(QWidget *parent) :  

  

    QLabel(parent)  

  

{  

}  

  

void ImageLabel::enterEvent(QEvent *) //随着鼠标移动会及时更新鼠标位置  

{  

    setMouseTracking(true);  

}  

  

void ImageLabel::mouseMoveEvent(QMouseEvent *ev){  

  

    customMouseMoveEvent(ev->x(),ev->y());  

}  

槽函数

[cpp] view
plaincopy

private slots :  

    void setMousePosition(int,int);  

槽函数实现:

[cpp] view
plaincopy

void CLASSX::setMousePosition(int x, int y){  

    qDebug()<<x<<" "<<y;  

}  

把信号和槽连接

[cpp] view
plaincopy

ImageLabel* imageLabel = new ImageLabel();  

connect(imageLabel,SIGNAL(customMouseMoveEvent(int,int)) ,  

                           this,SLOT(setMousePosition(int,int)));  

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

3.测试结果
随着鼠标在图片上移动出现了坐标的变化



            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: