QLabel Class

The QLabel widget provides a text or image display. More...

Header: #include <QLabel>
qmake: QT += widgets
Inherits: QFrame

Properties

Public Functions

Qt::Alignment alignment() const
bool hasScaledContents() const
bool hasSelectedText() const
int indent() const
int margin() const
bool openExternalLinks() const
const QPixmap *pixmap() const
QPixmap pixmap(Qt::ReturnByValueConstant) const
QString selectedText() const
void setAlignment(Qt::Alignment)
void setIndent(int)
void setMargin(int)
void setOpenExternalLinks(bool open)
void setScaledContents(bool)
void setTextFormat(Qt::TextFormat)
void setTextInteractionFlags(Qt::TextInteractionFlags flags)
void setWordWrap(bool on)
QString text() const
Qt::TextFormat textFormat() const
Qt::TextInteractionFlags textInteractionFlags() const
bool wordWrap() const

Public Slots

void setPixmap(const QPixmap &)
void setText(const QString &)

Detailed Description

QLabel is used for displaying text or an image. No user interaction functionality is provided. The visual appearance of the label can be configured in various ways, and it can be used for specifying a focus mnemonic key for another widget.

A QLabel can contain any of the following content types:

ContentSetting
Plain textPass a QString to setText().
Rich textPass a QString that contains rich text to setText().
A pixmapPass a QPixmap to setPixmap().
A moviePass a QMovie to setMovie().
A numberPass an int or a double to setNum(), which converts the number to plain text.
NothingThe same as an empty plain text. This is the default. Set by clear().

Warning: When passing a QString to the constructor or calling setText(), make sure to sanitize your input, as QLabel tries to guess whether it displays the text as plain text or as rich text, a subset of HTML 4 markup. You may want to call setTextFormat() explicitly, e.g. in case you expect the text to be in plain format but cannot control the text source (for instance when displaying data loaded from the Web).

When the content is changed using any of these functions, any previous content is cleared.

By default, labels display left-aligned, vertically-centered text and images, where any tabs in the text to be displayed are automatically expanded. However, the look of a QLabel can be adjusted and fine-tuned in several ways.

The positioning of the content within the QLabel widget area can be tuned with setAlignment() and setIndent(). Text content can also wrap lines along word boundaries with setWordWrap(). For example, this code sets up a sunken panel with a two-line text in the bottom right corner (both lines being flush with the right side of the label):

 QLabel *label = new QLabel(this);
 label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
 label->setText("first line\nsecond line");
 label->setAlignment(Qt::AlignBottom | Qt::AlignRight);

The properties and functions QLabel inherits from QFrame can also be used to specify the widget frame to be used for any given label.

A QLabel is often used as a label for an interactive widget. For this use QLabel provides a useful mechanism for adding an mnemonic (see QKeySequence) that will set the keyboard focus to the other widget (called the QLabel's "buddy"). For example:

 QLineEdit *phoneEdit = new QLineEdit(this);
 QLabel *phoneLabel = new QLabel("&Phone:", this);
 phoneLabel->setBuddy(phoneEdit);

In this example, keyboard focus is transferred to the label's buddy (the QLineEdit) when the user presses Alt+P. If the buddy was a button (inheriting from QAbstractButton), triggering the mnemonic would emulate a button click.

See also QLineEdit, QTextEdit, QPixmap, QMovie, and GUI Design Handbook: Label.

Property Documentation

alignment : Qt::Alignment

This property holds the alignment of the label's contents

By default, the contents of the label are left-aligned and vertically-centered.

Access functions:

Qt::Alignment alignment() const
void setAlignment(Qt::Alignment)

See also text.

hasSelectedText : const bool

This property holds whether there is any text selected

hasSelectedText() returns true if some or all of the text has been selected by the user; otherwise returns false.

By default, this property is false.

Note: The textInteractionFlags set on the label need to include either TextSelectableByMouse or TextSelectableByKeyboard.

This property was introduced in Qt 4.7.

Access functions:

bool hasSelectedText() const

See also selectedText().

indent : int

This property holds the label's text indent in pixels

If a label displays text, the indent applies to the left edge if alignment() is Qt::AlignLeft, to the right edge if alignment() is Qt::AlignRight, to the top edge if alignment() is Qt::AlignTop, and to the bottom edge if alignment() is Qt::AlignBottom.

If indent is negative, or if no indent has been set, the label computes the effective indent as follows: If frameWidth() is 0, the effective indent becomes 0. If frameWidth() is greater than 0, the effective indent becomes half the width of the "x" character of the widget's current font().

By default, the indent is -1, meaning that an effective indent is calculating in the manner described above.

Access functions:

int indent() const
void setIndent(int)

See also alignment, margin, frameWidth(), and font().

margin : int

This property holds the width of the margin

The margin is the distance between the innermost pixel of the frame and the outermost pixel of contents.

The default margin is 0.

Access functions:

int margin() const
void setMargin(int)

See also indent.

Specifies whether QLabel should automatically open links using QDesktopServices::openUrl() instead of emitting the linkActivated() signal.

Note: The textInteractionFlags set on the label need to include either LinksAccessibleByMouse or LinksAccessibleByKeyboard.

The default value is false.

This property was introduced in Qt 4.2.

Access functions:

bool openExternalLinks() const
void setOpenExternalLinks(bool open)

See also textInteractionFlags().

pixmap : QPixmap

This property holds the label's pixmap.

Previously, Qt provided a version of pixmap() which returned the pixmap by-pointer. That version is now deprecated. To maintain compatibility with old code, you can explicitly differentiate between the by-pointer function and the by-value function:

 const QPixmap *pixmapPtr = label->pixmap();
 QPixmap pixmapVal = label->pixmap(Qt::ReturnByValue);

If you disable the deprecated version using the QT_DISABLE_DEPRECATED_BEFORE macro, then you can omit Qt::ReturnByValue as shown below:

 QPixmap pixmapVal = label->pixmap();

If no pixmap has been set, the deprecated getter function will return nullptr.

Setting the pixmap clears any previous content. The buddy shortcut, if any, is disabled.

Access functions:

const QPixmap *pixmap() const
QPixmap pixmap(Qt::ReturnByValueConstant) const
void setPixmap(const QPixmap &)

scaledContents : bool

This property holds whether the label will scale its contents to fill all available space.

When enabled and the label shows a pixmap, it will scale the pixmap to fill the available space.

This property's default is false.

Access functions:

bool hasScaledContents() const
void setScaledContents(bool)

selectedText : const QString

This property holds the selected text

If there is no selected text this property's value is an empty string.

By default, this property contains an empty string.

Note: The textInteractionFlags set on the label need to include either TextSelectableByMouse or TextSelectableByKeyboard.

This property was introduced in Qt 4.7.

Access functions:

QString selectedText() const

See also hasSelectedText().

text : QString

This property holds the label's text

If no text has been set this will return an empty string. Setting the text clears any previous content.

The text will be interpreted either as plain text or as rich text, depending on the text format setting; see setTextFormat(). The default setting is Qt::AutoText; i.e. QLabel will try to auto-detect the format of the text set. See Supported HTML Subset for the definition of rich text.

If a buddy has been set, the buddy mnemonic key is updated from the new text.

Note that QLabel is well-suited to display small rich text documents, such as small documents that get their document specific settings (font, text color, link color) from the label's palette and font properties. For large documents, use QTextEdit in read-only mode instead. QTextEdit can also provide a scroll bar when necessary.

Note: This function enables mouse tracking if text contains rich text.

Access functions:

QString text() const
void setText(const QString &)

See also setTextFormat(), setBuddy(), and alignment.

textFormat : Qt::TextFormat

This property holds the label's text format

See the Qt::TextFormat enum for an explanation of the possible options.

The default format is Qt::AutoText.

Access functions:

Qt::TextFormat textFormat() const
void setTextFormat(Qt::TextFormat)

See also text().

textInteractionFlags : Qt::TextInteractionFlags

Specifies how the label should interact with user input if it displays text.

If the flags contain Qt::LinksAccessibleByKeyboard the focus policy is also automatically set to Qt::StrongFocus. If Qt::TextSelectableByKeyboard is set then the focus policy is set to Qt::ClickFocus.

The default value is Qt::LinksAccessibleByMouse.

This property was introduced in Qt 4.2.

Access functions:

Qt::TextInteractionFlags textInteractionFlags() const
void setTextInteractionFlags(Qt::TextInteractionFlags flags)

wordWrap : bool

This property holds the label's word-wrapping policy

If this property is true then label text is wrapped where necessary at word-breaks; otherwise it is not wrapped at all.

By default, word wrap is disabled.

Access functions:

bool wordWrap() const
void setWordWrap(bool on)

See also text.