QT为QLabel添加Click事件

来源:互联网 发布:神奇的水晶球软件 编辑:程序博客网 时间:2024/05/29 03:09

ClickedLabel.h

[cpp] view plain copy
print?
  1. #ifndef CLICKEDLABEL_H_  
  2. #define CLICKEDLABEL_H_  
  3. #include <QLabel>  
  4. #include <QWidget>  
  5. class ClickedLabel : public QLabel  
  6. {  
  7.     Q_OBJECT  
  8. signals:  
  9.     void Clicked(ClickedLabel* clicked);  
  10. public:  
  11.     ClickedLabel(QWidget *parent=0): QLabel(parent),m_str("")  
  12.     {  
  13.         setText(m_str);  
  14.     };  
  15.     ~ClickedLabel() {};  
  16. protected:  
  17.     void mouseReleaseEvent( QMouseEvent* );  
  18. private:  
  19.     QString m_str;  
  20. };  
  21. #endif /* CLICKEDLABEL_H_ */  

 

ClickedLabel.cpp

[cpp] view plain copy
print?
  1. #include "ClickedLabel.h"  
  2. void ClickedLabel::mouseReleaseEvent(QMouseEvent *evt)  
  3. {  
  4.     emit Clicked(this);  
  5. }  
原创粉丝点击