zoukankan      html  css  js  c++  java
  • QT为QLabel添加Click事件(如果我们使用组件,我们关心的是信号槽;如果我们自定义组件,我们关心的是事件)

    其实就是改写了一个函数:mouseReleaseEvent,当在QLabel放开鼠标的时,就发射点击信号。

    [cpp] view plaincopy
     
    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_ */  
    [cpp] view plaincopy
     
    1. #include "ClickedLabel.h"  
    2. void ClickedLabel::mouseReleaseEvent(QMouseEvent *evt)  
    3. {  
    4.     emit Clicked(this);  
    5. }  

    参考:http://blog.csdn.net/tingsking18/article/details/4071619

    http://qt-project.org/wiki/Make-a-QLabel-Clickable

    --------------------------------------------------------------------------------

    总的来说,如果我们使用组件,我们关心的是信号槽;如果我们自定义组件,我们关心的是事件。因为我们可以通过事件来改变组件的默认操作。比如,如果我们要自定义一个能够响应鼠标事件的EventLabel,我们就需要重写QLabel的鼠标事件,做出我们希望的操作,有可能还得在恰当的时候发出一个类似按钮的clicked()信号(如果我们希望让这个EventLabel能够被其它组件使用)或者其它的信号。

    摘自豆子博客第18节《事件》

  • 相关阅读:
    apue-ubuntu环境搭建
    visualgdb 调试arm
    CMake速记
    umask
    转换函数conversion function
    c++ hex string array 转换 串口常用
    tcp与串口透传(select)
    sqlite3数据库修复SQLite-database disk image is malformed
    container_of宏
    shell 入门学习
  • 原文地址:https://www.cnblogs.com/findumars/p/4058379.html
Copyright © 2011-2022 走看看