![]() |
Home · All Classes · All Functions · Overviews |
The QStaticText class enables optimized drawing of text when the text and its layout is updated rarely. More...
#include <QStaticText>
This class was introduced in Qt 4.7.
enum | PerformanceHint { ModerateCaching, AggressiveCaching } |
QStaticText () | |
QStaticText ( const QString & text, const QSizeF & size = QSizeF() ) | |
QStaticText ( const QStaticText & other ) | |
~QStaticText () | |
QSizeF | maximumSize () const |
PerformanceHint | performanceHint () const |
void | prepare ( const QTransform & matrix, const QFont & font ) |
void | setMaximumSize ( const QSizeF & size ) |
void | setPerformanceHint ( PerformanceHint performanceHint ) |
void | setText ( const QString & text ) |
void | setTextFormat ( Qt::TextFormat textFormat ) |
QSizeF | size () const |
QString | text () const |
Qt::TextFormat | textFormat () const |
bool | operator!= ( const QStaticText & other ) const |
QStaticText & | operator= ( const QStaticText & other ) |
bool | operator== ( const QStaticText & other ) const |
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 maximum size. The text will then occupy a single line.
If you set a maximum size on the QStaticText object, this will bound the text. The text will be formatted so that no line exceeds the given width. When the object is painted, it will be clipped at the given size. 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.
QStaticText will attempt to guess the format of the input text using Qt::mightBeRichText(). 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.
See also QPainter::drawText(), QPainter::drawStaticText(), QTextLayout, and QTextDocument.
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 QGLWidget.
Constant | Value | Description |
---|---|---|
QStaticText::ModerateCaching | 0 | Do basic caching for high performance at a low memory cost. |
QStaticText::AggressiveCaching | 1 | Use additional caching when available. This may improve performance at a higher memory cost. |
Constructs an empty QStaticText
Constructs a QStaticText object with the given text and bounded by the given size.
If an invalid size is passed for size the text will be unbounded.
Constructs a QStaticText object which is a copy of other.
Destroys the QStaticText.
Returns the maximum size of the QStaticText.
See also setMaximumSize().
Returns which performance hint is set for the QStaticText.
See also setPerformanceHint().
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 the painter's font or matrix is different from the one used for the currently cached layout. By default, QStaticText will use a default constructed QFont and an identity matrix to create its layout.
To avoid the overhead of creating the layout the first time you draw the QStaticText with a painter whose matrix or font are different from the defaults, 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() and QPainter::setMatrix().
Sets the maximum size of the QStaticText to size.
Note: This function will cause the layout of the text to be recalculated.
See also maximumSize() and size().
Sets the performance hint of the QStaticText. This hint can be 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 be recalculated.
See also performanceHint().
Sets the text of the QStaticText to text.
Note: This function will cause the layout of the text to be recalculated.
See also text().
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 be recalculated.
See also textFormat(), setText(), and text().
Returns the size of the bounding rect for this QStaticText.
See also maximumSize().
Returns the text of the QStaticText.
See also setText().
Returns the text format of the QStaticText.
See also setTextFormat(), setText(), and text().
Compares other to this QStaticText. Returns true if the texts, fonts or maximum sizes are different.
Assigns other to this QStaticText.
Compares other to this QStaticText. Returns true if the texts, fonts and maximum sizes are equal.
Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies) | Trademarks | Qt 4.7.0 |