Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Clickable QLabel: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=A ‘Click’able QLabel=
h1. A 'Click'able QLabel


A “clicked” signal may sometimes be required from a label, but there is no “clicked” signal emitted by QLabel.<br /> 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’.<br /> 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 ==


==Source==
<code>class ClickableLabel : public QLabel<br />{


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.
Q_OBJECT


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


* [[:Category:Developing with Qt|Developing_with_Qt]]
signals:<br /> void clicked();
* [[:Category:snippets|snippets]]
 
protected:<br /> void mousePressEvent ( QMouseEvent * event ) ;<br />};
 
</code>
 
== Source ==
 
<code><br />ClickableLabel::ClickableLabel( const QString&amp;amp; text, QWidget * parent ) :<br /> QLabel(parent)
 
{<br /> this-&gt;setText(text);<br /> }
 
ClickableLabel::~ClickableLabel()<br /> {<br /> }
 
void ClickableLabel::mousePressEvent ( QMouseEvent * event )
 
{<br /> emit clicked();<br /> }<br /></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.
 
[[Category:Developing_with_Qt]]

Revision as of 14:31, 23 February 2015

h1. A 'Click'able QLabel

A "clicked&quot; signal may sometimes be required from a label, but there is no "clicked&quot; 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<br />{

Q_OBJECT

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

signals:<br /> void clicked();

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

Source

<br />ClickableLabel::ClickableLabel( const QString&amp;amp; text, QWidget * parent ) :<br /> QLabel(parent)

{<br /> this-&gt;setText(text);<br /> }

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

void ClickableLabel::mousePressEvent ( QMouseEvent * event )

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

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.