A Clickable QLabel!


A "clicked" signal is often required from a label.
A QPushButton can be appeared like a label,by setting the 'flat' property.and that would be the best way.

Here is the code snippet for a basic custom QLable. Clickable QLabel.



// Header

class ourLabel : public QLabel
{

Q_OBJECT

public:
    explicit ourLabel( const QString& text ="", QWidget * parent = 0 );
    ~ourLabel();

signals:
    void clicked();

protected:
    void mousePressEvent ( QMouseEvent * event ) ;
};


// Source


ourLabel::ourLabel( const QString& text, QWidget * parent ) :
    QLabel(parent)

  {
      setText(text);
  }

  ourLabel::~ourLabel()
  {
  }

  void ourLabel::mousePressEvent ( QMouseEvent * event )

  {
      emit clicked();
  }


What we do here is simple : Catch the mouse press event on the label. Then emit 'clicked' signal

No comments:

Post a Comment