鼠标悬停、事件触发试用,及信号发送
2013-10-23 20:34
621 查看
鼠标悬停、事件触发试用,及信号发送
分类: Qt2012-05-1816: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.测试结果
随着鼠标在图片上移动出现了坐标的变化
![](http://my.csdn.net/uploads/201205/18/1337330923_7953.gif)
相关文章推荐
- mysql相关语句笔记
- poj 2115 C Looooops His love for her is enshrined forever in his heart.扩展欧几里得
- 单击一个颜色名或者 16 进制值,就可以查看与不同文字颜色搭配的背景颜色。
- java.lang.NoClassDefFoundError
- Unity界面插件NGUI核心组件说明
- 结构体的内存分配
- 在ubuntu12.04上用qvfb运行QtE程序
- 在DirectX 中进行2D渲染
- 项目中用到了的一些批处理文件
- hdu-2044-一只小蜜蜂...
- 常用服务器模型总结
- 将Request.QueryString获得的值转换为INT
- android json解析及简单例子
- C++ 随机函数
- ALSA声卡驱动中的DAPM详解之二:widget-具备路径和电源管理信息的kcontrol
- 使用python进行加密解密AES算法
- dyld: DYLD_ environment variables being ignored because main executable (/usr/bin/sudo) is setuid or setgid
- 指针高级
- C# 线程有参和无参启动
- FMDB数据库使用