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
(Decode HTML entity names)
(Fix code formatting)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{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 'Click'able 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!
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'.
== Header ==
In other words, a Clickable QLabel!


== Header ==
<nowiki>
#ifndef CLICKABLELABEL_H
#define CLICKABLELABEL_H


<code>class ClickableLabel : public QLabel
#include <QLabel>
{
#include <QWidget>
#include <Qt>


Q_OBJECT
class ClickableLabel : public QLabel {
    Q_OBJECT  


public:
public:
explicit ClickableLabel( const QString& text ="", QWidget * parent = 0 );
    explicit ClickableLabel(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
~ClickableLabel();
    ~ClickableLabel();


signals:
signals:
void clicked();
    void clicked();


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


</code>
#endif // CLICKABLELABEL_H
 
</nowiki>


== Source ==
== Source ==


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


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


ClickableLabel::~ClickableLabel()
ClickableLabel::~ClickableLabel() {}
{
}


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


{
</nowiki>
emit clicked();
}
</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]]

Latest revision as of 13:32, 29 January 2021

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.