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
AutoSpider (talk | contribs) (Convert ExpressionEngine section headers) |
AutoSpider (talk | contribs) (Decode HTML entity names) |
||
Line 16: | Line 16: | ||
public: | public: | ||
explicit ClickableLabel( const QString& | explicit ClickableLabel( const QString& text ="", QWidget * parent = 0 ); | ||
~ClickableLabel(); | ~ClickableLabel(); | ||
Line 31: | Line 31: | ||
<code> | <code> | ||
ClickableLabel::ClickableLabel( const QString& | ClickableLabel::ClickableLabel( const QString& text, QWidget * parent ) : | ||
QLabel(parent) | QLabel(parent) | ||
Revision as of 17:26, 12 March 2015
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine. Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean. |
A 'Click'able QLabel
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)
{
this->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.