4. Display Widgets

Display widgets are read-only widgets used to provide information to the user. They include

  • Qlabel for text, images, and animations,
  • QProgressBar for showing task progress,
  • QLCDNumber for displaying number in an LCD-style,
  • QTextBrowser for read-only rich text with hyperlink support.

4.1 Displaying Text with QLabel

QLabel supports three text formats: plain text, Markdown, and rich text (rendered using a subset of HTML). The format is controlled via setTextFormat(). Available constants are:

Value Enum Constant Description
0 Qt::PlainText Plain text
1 Qt::RichText Qt-supported HTML subset
2 Qt::AutoText Format automatically detected (default)
3 Qt::MarkdownText Markdown formatting
An icon of a clipboard-list1

To display text using QLabel:

  1. Create a QLabel object, setting its text. We create three labels:

    • one with plain text (no formatting),
    • one with Markdown syntax,
    • one with HTML syntax.
  2. Set the text format for the Markdown and rich-text labels using setTextFormat():

    • Qt.TextFormat.MarkdownText for the Markdown text,
    • Qt.TextFormat.RichText for the rich-text label.
  3. Add the label(s) to the layout and display the window. When the window is shown, the Markdown and rich-text labels will display bold text.

When you run the application you should see a window like this:

4.2 Displaying Images with Qlabel

QLabel can display static images, vector graphics, and animated GIFs using the following methods:

Method Description
setPixmap() Displays a static image from a QPixmap
setPicture() Displays a vector graphic from a QPicture
setMovie() Displays an animated GIF or movie from a QMovie
An icon of a clipboard-list1

To display an image in a label:

  1. Create an image object. We create two pictures: a png image using QPixmap and an animated gif image using QMovie,

  2. Create label objects that will display the images.

  3. Set the labels’ contents to the png and the gif images.

The running application should look like this:

4.3 Displaying LCD-like Numbers with QLCDNumber

As the documentation states, QLCDNumber is the very oldest part of Qt, tracing its roots back to a BASIC program on the Sinclair Spectrum.

An icon of a clipboard-list1

To use it in your application:

  1. Create a QLCDNumber instance,

  2. Set its digit count,

  3. Set the number to display using QLCDNumber.display()

When you start the application, you should see this:

You are not restricted to QLabel for images - any widget can draw images by reimplementing its paintEvent() method, giving you full control over rendering.