Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QStaticText Class Reference

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

#include <qstatictext.h>

+ Collaboration diagram for QStaticText:

Public Types

enum  PerformanceHint { ModerateCaching , AggressiveCaching }
 This enum the different performance hints that can be set on the QStaticText. More...
 

Public Member Functions

 QStaticText ()
 Constructs an empty QStaticText.
 
 QStaticText (const QString &text)
 Constructs a QStaticText object with the given text.
 
 QStaticText (const QStaticText &other)
 Constructs a QStaticText object which is a copy of other.
 
QStaticTextoperator= (const QStaticText &)
 Assigns other to this QStaticText.
 
 ~QStaticText ()
 Destroys the QStaticText.
 
void swap (QStaticText &other) noexcept
 
void setText (const QString &text)
 Sets the text of the QStaticText to text.
 
QString text () const
 Returns the text of the QStaticText.
 
void setTextFormat (Qt::TextFormat textFormat)
 Sets the text format of the QStaticText to textFormat.
 
Qt::TextFormat textFormat () const
 Returns the text format of the QStaticText.
 
void setTextWidth (qreal textWidth)
 Sets the preferred width for this QStaticText.
 
qreal textWidth () const
 Returns the preferred width for this QStaticText.
 
void setTextOption (const QTextOption &textOption)
 Sets the text option structure that controls the layout process to the given textOption.
 
QTextOption textOption () const
 Returns the current text option used to control the layout process.
 
QSizeF size () const
 Returns the size of the bounding rect for this QStaticText.
 
void prepare (const QTransform &matrix=QTransform(), const QFont &font=QFont())
 Prepares the QStaticText object for being painted with the given matrix and the given font to avoid overhead when the actual drawStaticText() call is made.
 
void setPerformanceHint (PerformanceHint performanceHint)
 Sets the performance hint of the QStaticText according to the performanceHint provided.
 
PerformanceHint performanceHint () const
 Returns which performance hint is set for the QStaticText.
 
bool operator== (const QStaticText &) const
 Compares other to this QStaticText.
 
bool operator!= (const QStaticText &) const
 Compares other to this QStaticText.
 

Friends

class QStaticTextPrivate
 

Detailed Description

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

Since
4.7 \inmodule QtGui

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 = nullptr) : QWidget(parent), m_staticText("This is static text")
protected:
void paintEvent(QPaintEvent *)
{
painter.drawStaticText(0, 0, m_staticText);
}
private:
QStaticText m_staticText;
};
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:486
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
void drawStaticText(const QPointF &topLeftPosition, const QStaticText &staticText)
The QStaticText class enables optimized drawing of text when the text and its layout is updated rarel...
Definition qstatictext.h:21
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
QPainter painter(this)
[7]

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, QTextDocument

Definition at line 20 of file qstatictext.h.

Member Enumeration Documentation

◆ 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.

\value ModerateCaching Do basic caching for high performance at a low memory cost. \value AggressiveCaching Use additional caching when available. This may improve performance at a higher memory cost.

Enumerator
ModerateCaching 
AggressiveCaching 

Definition at line 23 of file qstatictext.h.

Constructor & Destructor Documentation

◆ QStaticText() [1/3]

QStaticText::QStaticText ( )

Constructs an empty QStaticText.

Definition at line 122 of file qstatictext.cpp.

◆ QStaticText() [2/3]

QStaticText::QStaticText ( const QString & text)
explicit

Constructs a QStaticText object with the given text.

Definition at line 130 of file qstatictext.cpp.

References text().

+ Here is the call graph for this function:

◆ QStaticText() [3/3]

QStaticText::QStaticText ( const QStaticText & other)

Constructs a QStaticText object which is a copy of other.

Definition at line 140 of file qstatictext.cpp.

References other().

+ Here is the call graph for this function:

◆ ~QStaticText()

QStaticText::~QStaticText ( )

Destroys the QStaticText.

Definition at line 148 of file qstatictext.cpp.

References Q_ASSERT.

Member Function Documentation

◆ operator!=()

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

Compares other to this QStaticText.

Returns true if the texts, fonts or maximum sizes are different.

Definition at line 219 of file qstatictext.cpp.

References other().

+ Here is the call graph for this function:

◆ operator=()

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

Assigns other to this QStaticText.

Definition at line 189 of file qstatictext.cpp.

References other().

+ Here is the call graph for this function:

◆ operator==()

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

Compares other to this QStaticText.

Returns true if the texts, fonts and text widths are equal.

Definition at line 207 of file qstatictext.cpp.

References other().

+ Here is the call graph for this function:

◆ performanceHint()

QStaticText::PerformanceHint QStaticText::performanceHint ( ) const

Returns which performance hint is set for the QStaticText.

See also
setPerformanceHint()

Definition at line 304 of file qstatictext.cpp.

References AggressiveCaching, and ModerateCaching.

Referenced by setPerformanceHint().

+ Here is the caller graph for this function:

◆ prepare()

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

Prepares the QStaticText object for being painted with the given matrix and the given font to avoid overhead when the actual drawStaticText() call is made.

When drawStaticText() is called, the layout of the QStaticText will be recalculated if any part of the QStaticText object has changed since the last time it was drawn. It will also be recalculated if the painter's font is not the same as when the QStaticText was last drawn, or, on any other paint engine than the OpenGL2 engine, if the painter's matrix has been altered since the static text was last drawn.

To avoid the overhead of creating the layout the first time you draw the QStaticText after making changes, you can use the prepare() function and pass in the matrix and font you expect to use when drawing the text.

See also
QPainter::setFont(), QPainter::setWorldTransform()

Definition at line 178 of file qstatictext.cpp.

References font.

Referenced by QtWaylandClient::QWaylandAdwaitaDecoration::paint(), and QtWaylandClient::QWaylandBradientDecoration::paint().

+ Here is the caller graph for this function:

◆ setPerformanceHint()

void QStaticText::setPerformanceHint ( PerformanceHint performanceHint)

Sets the performance hint of the QStaticText according to the performanceHint provided.

The performanceHint is used to customize how much caching is done internally to improve performance.

The default is QStaticText::ModerateCaching.

Note
This function will cause the layout of the text to require recalculation.
See also
performanceHint()

Definition at line 288 of file qstatictext.cpp.

References AggressiveCaching, ModerateCaching, and performanceHint().

+ Here is the call graph for this function:

◆ setText()

void QStaticText::setText ( const QString & text)

Sets the text of the QStaticText to text.

Note
This function will cause the layout of the text to require recalculation.
See also
text()

Definition at line 231 of file qstatictext.cpp.

References text().

Referenced by QtWaylandClient::QWaylandAdwaitaDecoration::paint(), and QtWaylandClient::QWaylandBradientDecoration::paint().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setTextFormat()

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

Sets the text format of the QStaticText to textFormat.

If textFormat is set to Qt::AutoText (the default), the format of the text will try to be determined using the function Qt::mightBeRichText(). If the text format is Qt::PlainText, then the text will be displayed as is, whereas it will be interpreted as HTML if the format is Qt::RichText. HTML tags that alter the font of the text, its color, or its layout are supported by QStaticText.

Note
This function will cause the layout of the text to require recalculation.
See also
textFormat(), setText(), text()

Definition at line 249 of file qstatictext.cpp.

References textFormat().

Referenced by QtWaylandClient::QWaylandBradientDecoration::QWaylandBradientDecoration().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setTextOption()

void QStaticText::setTextOption ( const QTextOption & textOption)

Sets the text option structure that controls the layout process to the given textOption.

See also
textOption()

Definition at line 314 of file qstatictext.cpp.

References textOption().

Referenced by QtWaylandClient::QWaylandBradientDecoration::QWaylandBradientDecoration().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setTextWidth()

void QStaticText::setTextWidth ( qreal textWidth)

Sets the preferred width for this QStaticText.

If the text is wider than the specified width, it will be broken into multiple lines and grow vertically. If the text cannot be split into multiple lines, it will be larger than the specified textWidth.

Setting the preferred text width to a negative number will cause the text to be unbounded.

Use size() to get the actual size of the text.

Note
This function will cause the layout of the text to require recalculation.
See also
textWidth(), size()

Definition at line 342 of file qstatictext.cpp.

References textWidth().

+ Here is the call graph for this function:

◆ size()

QSizeF QStaticText::size ( ) const

Returns the size of the bounding rect for this QStaticText.

See also
textWidth()

Definition at line 364 of file qstatictext.cpp.

Referenced by QtWaylandClient::QWaylandAdwaitaDecoration::paint(), and QtWaylandClient::QWaylandBradientDecoration::paint().

+ Here is the caller graph for this function:

◆ swap()

void QStaticText::swap ( QStaticText & other)
inlinenoexcept
Since
5.0

Swaps this static text instance with other. This function is very fast and never fails.

Definition at line 35 of file qstatictext.h.

References other().

+ Here is the call graph for this function:

◆ text()

QString QStaticText::text ( ) const

Returns the text of the QStaticText.

See also
setText()

Definition at line 271 of file qstatictext.cpp.

Referenced by QStaticText(), QtWaylandClient::QWaylandAdwaitaDecoration::paint(), QtWaylandClient::QWaylandBradientDecoration::paint(), and setText().

+ Here is the caller graph for this function:

◆ textFormat()

Qt::TextFormat QStaticText::textFormat ( ) const

Returns the text format of the QStaticText.

See also
setTextFormat(), setText(), text()

Definition at line 261 of file qstatictext.cpp.

Referenced by setTextFormat().

+ Here is the caller graph for this function:

◆ textOption()

QTextOption QStaticText::textOption ( ) const

Returns the current text option used to control the layout process.

Definition at line 324 of file qstatictext.cpp.

Referenced by setTextOption().

+ Here is the caller graph for this function:

◆ textWidth()

qreal QStaticText::textWidth ( ) const

Returns the preferred width for this QStaticText.

See also
setTextWidth()

Definition at line 354 of file qstatictext.cpp.

Referenced by setTextWidth().

+ Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ QStaticTextPrivate

friend class QStaticTextPrivate
friend

Definition at line 63 of file qstatictext.h.


The documentation for this class was generated from the following files: