Clickable QLabel: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Added includes, changed constructor)
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
h1. A 'Click'able QLabel
{{LangSwitch}}
A "clicked" signal may sometimes be required from a label, but there is no "clicked" signal emitted by QLabel. You can work around this easily by making a QPushButton like a label by setting the 'flat' property.


A &quot;clicked&amp;quot; signal may sometimes be required from a label, but there is no &quot;clicked&amp;quot; signal emitted by QLabel.<br />You can work around this easily by making a QPushButton like a label by setting the 'flat' property.
However, if there are other properties of a QLabel object that you need, here is a code snippet for a custom QLabel which can emit a signal: 'clicked'. In other words, a Clickable QLabel!


However, if there are other properties of a QLabel object that you need, here is a code snippet for a custom QLabel which can emit a signal : 'clicked'.<br />In other words, a Clickable QLabel!
== Header ==


== Header ==
<code>
#ifndef CLICKABLELABEL_H
#define CLICKABLELABEL_H
 
#include <QLabel>
#include <QWidget>
#include <Qt>
 
class ClickableLabel : public QLabel {
    Q_OBJECT


<code>class ClickableLabel : public QLabel<br />{
public:
    explicit ClickableLabel(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
    ~ClickableLabel();


Q_OBJECT
signals:
    void clicked();


public:<br /> explicit ClickableLabel( const QString&amp;amp; text =&quot;&quot;, QWidget * parent = 0 );<br /> ~ClickableLabel();
protected:
    void mousePressEvent(QMouseEvent* event);


signals:<br /> void clicked();
};


protected:<br /> void mousePressEvent ( QMouseEvent * event ) ;<br />};
#endif // CLICKABLELABEL_H


</code>
</code>
Line 21: Line 35:
== Source ==
== Source ==


<code><br />ClickableLabel::ClickableLabel( const QString&amp;amp; text, QWidget * parent ) :<br /> QLabel(parent)
<code>
#include "clickablelabel.h"


{<br /> this-&gt;setText(text);<br /> }
ClickableLabel::ClickableLabel(QWidget* parent, Qt::WindowFlags f)
    : QLabel(parent) {
   
}


ClickableLabel::~ClickableLabel()<br /> {<br /> }
ClickableLabel::~ClickableLabel() {}


void ClickableLabel::mousePressEvent ( QMouseEvent * event )
void ClickableLabel::mousePressEvent(QMouseEvent* event) {
    emit clicked();
}


{<br /> emit clicked();<br /> }<br /></code>
</code>


What we do here is simple : Catch the mouse press event on the label. Then emit 'clicked' signal. We could as well make the signal be emitted when mouse gets released. This is let to be a decision of the developer.
What we do here is simple: Catch the mouse press event on the label. Then emit 'clicked' signal. We could as well make the signal be emitted when mouse gets released. This is let to be a decision of the developer.


[[Category:Developing_with_Qt]]
[[Category:Developing_with_Qt]]

Revision as of 07:13, 18 May 2017

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

A "clicked" signal may sometimes be required from a label, but there is no "clicked" signal emitted by QLabel. You can work around this easily by making a QPushButton like a label by setting the 'flat' property.

However, if there are other properties of a QLabel object that you need, here is a code snippet for a custom QLabel which can emit a signal: 'clicked'. In other words, a Clickable QLabel!

Header

#ifndef CLICKABLELABEL_H
#define CLICKABLELABEL_H

#include <QLabel>
#include <QWidget>
#include <Qt>

class ClickableLabel : public QLabel { 
    Q_OBJECT 

public:
    explicit ClickableLabel(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
    ~ClickableLabel();

signals:
    void clicked();

protected:
    void mousePressEvent(QMouseEvent* event);

};

#endif // CLICKABLELABEL_H

Source

#include "clickablelabel.h"

ClickableLabel::ClickableLabel(QWidget* parent, Qt::WindowFlags f)
    : QLabel(parent) {
    
}

ClickableLabel::~ClickableLabel() {}

void ClickableLabel::mousePressEvent(QMouseEvent* event) {
    emit clicked();
}

What we do here is simple: Catch the mouse press event on the label. Then emit 'clicked' signal. We could as well make the signal be emitted when mouse gets released. This is let to be a decision of the developer.