6.0.0-rc4Source

QLabel Class

The QLabel class displays text. More…

Header#include <QLabel>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQWidget

List of all members, including inherited members

Public Functions

QLabel(QWidget *parent = nullptr)
QLabel(const QString &text, QWidget *parent = nullptr)
~QLabel()
QStringtext() const
Qt::Alignmentalignment() const
voidsetAlignment(Qt::Alignment alignment)
boolwordWrap() const
voidsetWordWrap(bool on)
intmargin() const
voidsetMargin(int margin)
QPixmappixmap() const
boolhasScaledContents() const
voidsetScaledContents(bool scaled)
Qt::TextFormattextFormat() const
voidsetTextFormat(Qt::TextFormat format)
boolopenExternalLinks() const
voidsetOpenExternalLinks(bool open)

Reimplemented Public Functions

Public Slots

voidsetText(const QString &text)
voidsetNum(int number)
voidsetPixmap(const QPixmap &pixmap)
voidclear()

Signals

voidlinkActivated(const QString &link)
voidlinkHovered(const QString &link)

Reimplemented Protected Functions

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.

QLabel in the light color scheme

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

[explicit] QLabel::QLabel(QWidget *parent = nullptr)

Constructs an empty label with the given parent.

[explicit] QLabel::QLabel(const QString &text, QWidget *parent = nullptr)

Constructs a label that shows text, with the given parent.

[override] 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().

[override, protected] void QLabel::changeEvent(QEvent *event)

Reimplements QWidget::changeEvent().

[slot] 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.

[override, protected] void QLabel::leaveEvent(QEvent *event)

Reimplements QWidget::leaveEvent().

[signal] 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().

[signal] 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().

[override] QSize QLabel::minimumSizeHint() const

Reimplements QWidget::minimumSizeHint().

[override, protected] void QLabel::mouseMoveEvent(QMouseEvent *event)

Reimplements QWidget::mouseMoveEvent().

Over a link the cursor is the pointing hand, and linkHovered() is emitted.

[override, protected] void QLabel::mousePressEvent(QMouseEvent *event)

Reimplements QWidget::mousePressEvent().

A press on a link is the start of a click on it.

[override, protected] void QLabel::mouseReleaseEvent(QMouseEvent *event)

Reimplements QWidget::mouseReleaseEvent().

A release on the link that was pressed emits linkActivated().

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().

[override, protected] 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().

[slot] void QLabel::setNum(int number)

Sets the text of the label to number, as a decimal number.

Sets whether the label is to open links to open; recorded only.

See also openExternalLinks().

[slot] 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.

[slot] 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().

[override] 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().