QLabel Class
The QLabel class displays text. More…
| Header | #include <QLabel> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QWidget |
List of all members, including inherited members
Public Functions
| QLabel(QWidget *parent = nullptr) | |
| QLabel(const QString &text, QWidget *parent = nullptr) | |
| ~QLabel() | |
| QString | text() const |
| Qt::Alignment | alignment() const |
| void | setAlignment(Qt::Alignment alignment) |
| bool | wordWrap() const |
| void | setWordWrap(bool on) |
| int | margin() const |
| void | setMargin(int margin) |
| QPixmap | pixmap() const |
| bool | hasScaledContents() const |
| void | setScaledContents(bool scaled) |
| Qt::TextFormat | textFormat() const |
| void | setTextFormat(Qt::TextFormat format) |
| bool | openExternalLinks() const |
| void | setOpenExternalLinks(bool open) |
Reimplemented Public Functions
| QSize | sizeHint() const |
| QSize | minimumSizeHint() const |
Public Slots
| void | setText(const QString &text) |
| void | setNum(int number) |
| void | setPixmap(const QPixmap &pixmap) |
| void | clear() |
Signals
| void | linkActivated(const QString &link) |
| void | linkHovered(const QString &link) |
Reimplemented Protected Functions
| void | paintEvent(QPaintEvent *event) |
| void | changeEvent(QEvent *event) |
| void | mousePressEvent(QMouseEvent *event) |
| void | mouseMoveEvent(QMouseEvent *event) |
| void | mouseReleaseEvent(QMouseEvent *event) |
| void | leaveEvent(QEvent *event) |
Detailed Description
A label shows a text, aligned in its rectangle. It does nothing else:
QLabel *label = new QLabel("Name:", parent);
label->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
The text is drawn in the WindowText color of the palette with the font of the widget. A label with word wrap breaks lines at the width it gets.
A text that looks like HTML is shown as rich text, through a QTextDocument, which says what is understood; setTextFormat() decides it for the text if it does not look like what it is. A link in the text shows the pointing hand, and a click on it emits linkActivated():
QLabel *label = new QLabel("<b>Done.</b> See the <a href=\"report\">report</a>.", parent);
QObject::connect(label, &QLabel::linkActivated, [](const QString &link) { open(link); });
A label: plain, bold and larger, word-wrapped, right-aligned, and disabled.

See also QPushButton and QWidget.
Differences from Qt
The label shows plain text, rich text or a pixmap: no movies, Markdown, selection of the text, links reached with the keys, or buddies. It has no frame (it is not a QFrame), indent(), textInteractionFlags() or setBuddy(). Nothing opens a link for openExternalLinks(): a click emits linkActivated() with or without it. Rich text is placed as a whole by the alignment of the label; its paragraphs are aligned by their own HTML. A word-wrapped label has no height-for-width, so its size hint is that of the text with the lines that it has without the wrapping.
Member Function Documentation
QLabel::QLabel(QWidget *parent = nullptr)
Constructs an empty label with the given parent.
QLabel::QLabel(const QString &text, QWidget *parent = nullptr)
Constructs a label that shows text, with the given parent.
QLabel::~QLabel()
Destroys the label.
Qt::Alignment QLabel::alignment() const
Returns the alignment of the text in the label. The default is left and vertically centered.
See also setAlignment().
void QLabel::changeEvent(QEvent *event)
Reimplements QWidget::changeEvent().
void QLabel::clear()
Clears the text of the label.
bool QLabel::hasScaledContents() const
Returns whether the pixmap is scaled to fill the label. The default is false.
void QLabel::leaveEvent(QEvent *event)
Reimplements QWidget::leaveEvent().
void QLabel::linkActivated(const QString &link)
This signal is emitted when the user clicks a link of the rich text of the label. link is the target of the link, the href of its a element.
See also linkHovered().
void QLabel::linkHovered(const QString &link)
This signal is emitted when the mouse comes over a link of the rich text of the label, with the target of the link as link, and when it leaves the link, with an empty string.
See also linkActivated().
int QLabel::margin() const
Returns the space between the text and the edge of the label, in pixels.
See also setMargin().
QSize QLabel::minimumSizeHint() const
Reimplements QWidget::minimumSizeHint().
void QLabel::mouseMoveEvent(QMouseEvent *event)
Reimplements QWidget::mouseMoveEvent().
Over a link the cursor is the pointing hand, and linkHovered() is emitted.
void QLabel::mousePressEvent(QMouseEvent *event)
Reimplements QWidget::mousePressEvent().
A press on a link is the start of a click on it.
void QLabel::mouseReleaseEvent(QMouseEvent *event)
Reimplements QWidget::mouseReleaseEvent().
A release on the link that was pressed emits linkActivated().
bool QLabel::openExternalLinks() const
Returns whether the label is to open the links that the user clicks. Nt has nothing to open them with, so this is recorded only.
See also setOpenExternalLinks() and linkActivated().
void QLabel::paintEvent(QPaintEvent *event)
Reimplements QWidget::paintEvent().
QPixmap QLabel::pixmap() const
Returns the pixmap of the label, or a null pixmap if it shows text.
See also setPixmap().
void QLabel::setAlignment(Qt::Alignment alignment)
Sets the alignment of the text to alignment.
See also alignment().
void QLabel::setMargin(int margin)
Sets the margin to margin pixels.
See also margin().
void QLabel::setNum(int number)
Sets the text of the label to number, as a decimal number.
void QLabel::setOpenExternalLinks(bool open)
Sets whether the label is to open links to open; recorded only.
See also openExternalLinks().
void QLabel::setPixmap(const QPixmap &pixmap)
Shows pixmap instead of the text, which is cleared. The pixmap is placed by the alignment, or scaled to the label if hasScaledContents() is true.
See also pixmap() and setText().
void QLabel::setScaledContents(bool scaled)
Sets whether the pixmap is scaled to fill the label to scaled.
void QLabel::setText(const QString &text)
Sets the text of the label to text.
See also text().
void QLabel::setTextFormat(Qt::TextFormat format)
Sets how the text of the label is read to format. Qt::MarkdownText is shown as plain text.
See also textFormat().
void QLabel::setWordWrap(bool on)
Sets whether lines are broken at the width of the label to on.
See also wordWrap().
QSize QLabel::sizeHint() const
Reimplements QWidget::sizeHint().
QString QLabel::text() const
Returns the text of the label.
See also setText().
Qt::TextFormat QLabel::textFormat() const
Returns how the text of the label is read. The default is Qt::AutoText: a text that Qt::mightBeRichText() takes for rich text is shown as rich text, and any other as it is.
See also setTextFormat().
bool QLabel::wordWrap() const
Returns whether lines are broken at the width of the label.
See also setWordWrap().