原文来源:
http://www.cnblogs.com/findumars/p/4058379.html 原理:
其实就是改写了一个函数:mouseReleaseEvent,当在QLabel放开鼠标的时,就发射点击信号。 .定义Label类
#ifndef CLICKEDLABEL_H_
#define CLICKEDLABEL_H_
#include <QLabel>
#include <QWidget>
class ClickedLabel : public QLabel
{
Q_OBJECT
signals:
void Clicked(ClickedLabel* clicked);
public:
ClickedLabel(QWidget *parent=): QLabel(parent),m_str("")
{
setText(m_str);
};
~ClickedLabel() {};
protected:
void mouseReleaseEvent( QMouseEvent* );
private:
QString m_str;
};
#endif /* CLICKEDLABEL_H_ */ .实现代码
#include "ClickedLabel.h"
void ClickedLabel::mouseReleaseEvent(QMouseEvent *evt)
{
emit Clicked(this);
}