Clickable QLabel: Difference between revisions
Jump to navigation
Jump to search
m (Wieland moved page Make-a-QLabel-Clickable to Clickable QLabel: underscores) |
(Cleanup) |
||
Line 1: | Line 1: | ||
{{ | {{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. | |||
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'. | |||
In other words, a Clickable QLabel! | |||
== Header == | == Header == | ||
<code>class ClickableLabel : public QLabel | <code> | ||
class ClickableLabel : public QLabel | |||
{ | { | ||
Q_OBJECT | Q_OBJECT | ||
public: | public: | ||
explicit ClickableLabel( const QString& text="", QWidget* parent=0 ); | |||
~ClickableLabel(); | |||
signals: | signals: | ||
void clicked(); | |||
protected: | protected: | ||
void mousePressEvent(QMouseEvent* event); | |||
}; | }; | ||
</code> | </code> | ||
Line 31: | Line 23: | ||
<code> | <code> | ||
ClickableLabel::ClickableLabel( const QString& text, QWidget * parent ) : | ClickableLabel::ClickableLabel(const QString& text, QWidget* parent) | ||
: QLabel(parent) | |||
{ | { | ||
setText(text); | |||
} | |||
ClickableLabel::~ClickableLabel() | ClickableLabel::~ClickableLabel() | ||
{ | |||
} | |||
void ClickableLabel::mousePressEvent(QMouseEvent* event) | |||
{ | { | ||
emit clicked(); | |||
} | |||
</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 22:01, 27 June 2015
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
class ClickableLabel : public QLabel
{
Q_OBJECT
public:
explicit ClickableLabel( const QString& text="", QWidget* parent=0 );
~ClickableLabel();
signals:
void clicked();
protected:
void mousePressEvent(QMouseEvent* event);
};
Source
ClickableLabel::ClickableLabel(const QString& text, QWidget* parent)
: QLabel(parent)
{
setText(text);
}
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.