QStaticText Class

The QStaticText class enables optimized drawing of text when the text and its layout is updated rarely. More...

Header: #include <QStaticText>
qmake: QT += gui
Since: Qt 4.7

Public Types

enum PerformanceHint { ModerateCaching, AggressiveCaching }

Public Functions

QStaticText()
QStaticText(const QString &text)
QStaticText(const QStaticText &other)
~QStaticText()
QStaticText::PerformanceHint performanceHint() const
void prepare(const QTransform &matrix = QTransform(), const QFont &font = QFont())
void setPerformanceHint(QStaticText::PerformanceHint performanceHint)
void setText(const QString &text)
void setTextFormat(Qt::TextFormat textFormat)
void setTextOption(const QTextOption &textOption)
void setTextWidth(qreal textWidth)
QSizeF size() const
void swap(QStaticText &other)
QString text() const
Qt::TextFormat textFormat() const
QTextOption textOption() const
qreal textWidth() const
bool operator!=(const QStaticText &) const
QStaticText &operator=(QStaticText &&other)
QStaticText &operator=(const QStaticText &)
bool operator==(const QStaticText &) const

Detailed Description

The QStaticText class enables optimized drawing of text when the text and its layout is updated rarely.

QStaticText provides a way to cache layout data for a block of text so that it can be drawn more efficiently than by using QPainter::drawText() in which the layout information is recalculated with every call.

The class primarily provides an optimization for cases where the text, its font and the transformations on the painter are static over several paint events. If the text or its layout is changed for every iteration, QPainter::drawText() is the more efficient alternative, since the static text's layout would have to be recalculated to take the new state into consideration.

Translating the painter will not cause the layout of the text to be recalculated, but will cause a very small performance impact on drawStaticText(). Altering any other parts of the painter's transformation or the painter's font will cause the layout of the static text to be recalculated. This should be avoided as often as possible to maximize the performance benefit of using QStaticText.

In addition, only affine transformations are supported by drawStaticText(). Calling drawStaticText() on a projected painter will perform slightly worse than using the regular drawText() call, so this should be avoided.


  class MyWidget: public QWidget
  {
  public:
      MyWidget(QWidget *parent = 0) : QWidget(parent), m_staticText("This is static text")

  protected:
      void paintEvent(QPaintEvent *)
      {
          QPainter painter(this);
          painter.drawStaticText(0, 0, m_staticText);
      }

  private:
      QStaticText m_staticText;
  };

The QStaticText class can be used to mimic the behavior of QPainter::drawText() to a specific point with no boundaries, and also when QPainter::drawText() is called with a bounding rectangle.

If a bounding rectangle is not required, create a QStaticText object without setting a preferred text width. The text will then occupy a single line.

If you set a text width on the QStaticText object, this will bound the text. The text will be formatted so that no line exceeds the given width. The text width set for QStaticText will not automatically be used for clipping. To achieve clipping in addition to line breaks, use QPainter::setClipRect(). The position of the text is decided by the argument passed to QPainter::drawStaticText() and can change from call to call with a minimal impact on performance.

For extra convenience, it is possible to apply formatting to the text using the HTML subset supported by QTextDocument. QStaticText will attempt to guess the format of the input text using Qt::mightBeRichText(), and interpret it as rich text if this function returns true. To force QStaticText to display its contents as either plain text or rich text, use the function QStaticText::setTextFormat() and pass in, respectively, Qt::PlainText and Qt::RichText.

QStaticText can only represent text, so only HTML tags which alter the layout or appearance of the text will be respected. Adding an image to the input HTML, for instance, will cause the image to be included as part of the layout, affecting the positions of the text glyphs, but it will not be displayed. The result will be an empty area the size of the image in the output. Similarly, using tables will cause the text to be laid out in table format, but the borders will not be drawn.

If it's the first time the static text is drawn, or if the static text, or the painter's font has been altered since the last time it was drawn, the text's layout has to be recalculated. On some paint engines, changing the matrix of the painter will also cause the layout to be recalculated. In particular, this will happen for any engine except for the OpenGL2 paint engine. Recalculating the layout will impose an overhead on the QPainter::drawStaticText() call where it occurs. To avoid this overhead in the paint event, you can call prepare() ahead of time to ensure that the layout is calculated.

See also QPainter::drawText(), QPainter::drawStaticText(), QTextLayout, and QTextDocument.

Member Type Documentation

enum QStaticText::PerformanceHint

This enum the different performance hints that can be set on the QStaticText. These hints can be used to indicate that the QStaticText should use additional caches, if possible, to improve performance at the expense of memory. In particular, setting the performance hint AggressiveCaching on the QStaticText will improve performance when using the OpenGL graphics system or when drawing to a QOpenGLWidget.

ConstantValueDescription
QStaticText::ModerateCaching0Do basic caching for high performance at a low memory cost.
QStaticText::AggressiveCaching1Use additional caching when available. This may improve performance at a higher memory cost.

Member Function Documentation

QStaticText::QStaticText()

Default constructs an instance of QStaticText.

QStaticText::QStaticText(const QString &text)

Default constructs an instance of QStaticText.

QStaticText::QStaticText(const QStaticText &other)

Default constructs an instance of QStaticText.

QStaticText::~QStaticText()

Destroys the instance of QStaticText.

QStaticText::PerformanceHint QStaticText::performanceHint() const

See also setPerformanceHint().

void QStaticText::prepare(const QTransform &matrix = QTransform(), const QFont &font = QFont())

void QStaticText::setPerformanceHint(QStaticText::PerformanceHint performanceHint)

See also performanceHint().

void QStaticText::setText(const QString &text)

See also text().

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

See also textFormat().

void QStaticText::setTextOption(const QTextOption &textOption)

See also textOption().

void QStaticText::setTextWidth(qreal textWidth)

See also textWidth().

QSizeF QStaticText::size() const

void QStaticText::swap(QStaticText &other)

QString QStaticText::text() const

See also setText().

Qt::TextFormat QStaticText::textFormat() const

See also setTextFormat().

QTextOption QStaticText::textOption() const

See also setTextOption().

qreal QStaticText::textWidth() const

See also setTextWidth().

bool QStaticText::operator!=(const QStaticText &) const

QStaticText &QStaticText::operator=(QStaticText &&other)

Move-assignment operator.

QStaticText &QStaticText::operator=(const QStaticText &)

Copy-assignment operator.

bool QStaticText::operator==(const QStaticText &) const