clickedLabel.h
#ifndef CLICKLABEL_H
#define CLICKLABEL_H
#include <QLabel>
#include <QMouseEvent>
class ClickLabel : public QLabel
{
Q_OBJECT
public:
explicit ClickLabel(QWidget *parent = 0);
~ClickLabel();
signals:
void clicked();//自定义clicked()信号,在mousePressEvent事件发生时触发
protected:
virtual void mousePressEvent(QMouseEvent* event);//重写mousePressEvent事件
};
#endif // CLICKLABEL_H
clickedLabel.cpp
#include "clicklabel.h"
ClickLabel::ClickLabel(QWidget* parent)
: QLabel(parent)
{
}
ClickLabel::~ClickLabel()
{
}
void ClickLabel::mousePressEvent(QMouseEvent *event)//重写mousePressEvent事件,检测事件类型是不是点击了鼠标左键
{
//如果单击了就触发clicked信号
if (event->button() == Qt::LeftButton) {
//触发clicked信号
emit clicked();
}
//将该事件传给父类处理
QLabel::mousePressEvent(event);
}
使用前把qlabel的类提升为ClickLabel
添加SLOT函数
private slots:
void clicklabel_clicked(); //定义槽函数处理label clicked
connect
connect(ui->setrefresh_img_label, SIGNAL(clicked()), this, SLOT(clicklabel_clicked()));
http://blog.csdn.net/linbounconstraint/article/details/52414013