Qt 4.8
Public Types | Public Functions | Private Functions | Properties | Friends | List of all members
QStaticText Class Reference

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

#include <qstatictext.h>

Public Types

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

Public Functions

bool operator!= (const QStaticText &) const
 Compares other to this QStaticText. More...
 
QStaticTextoperator= (const QStaticText &)
 Assigns other to this QStaticText. More...
 
bool operator== (const QStaticText &) const
 Compares other to this QStaticText. More...
 
PerformanceHint performanceHint () const
 Returns which performance hint is set for the QStaticText. More...
 
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. More...
 
 QStaticText ()
 Constructs an empty QStaticText. More...
 
 QStaticText (const QString &text)
 Constructs a QStaticText object with the given text. More...
 
 QStaticText (const QStaticText &other)
 Constructs a QStaticText object which is a copy of other. More...
 
void setPerformanceHint (PerformanceHint performanceHint)
 Sets the performance hint of the QStaticText according to the performanceHint provided. More...
 
void setText (const QString &text)
 Sets the text of the QStaticText to text. More...
 
void setTextFormat (Qt::TextFormat textFormat)
 Sets the text format of the QStaticText to textFormat. More...
 
void setTextOption (const QTextOption &textOption)
 Sets the text option structure that controls the layout process to the given textOption. More...
 
void setTextWidth (qreal textWidth)
 Sets the preferred width for this QStaticText. More...
 
QSizeF size () const
 Returns the size of the bounding rect for this QStaticText. More...
 
QString text () const
 Returns the text of the QStaticText. More...
 
Qt::TextFormat textFormat () const
 Returns the text format of the QStaticText. More...
 
QTextOption textOption () const
 Returns the current text option used to control the layout process. More...
 
qreal textWidth () const
 Returns the preferred width for this QStaticText. More...
 
 ~QStaticText ()
 Destroys the QStaticText. More...
 

Private Functions

void detach ()
 

Properties

QExplicitlySharedDataPointer< QStaticTextPrivatedata
 

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

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

Definition at line 60 of file qstatictext.h.

Enumerations

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

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

Definition at line 63 of file qstatictext.h.

Constructors and Destructors

◆ QStaticText() [1/3]

QStaticText::QStaticText ( )

Constructs an empty QStaticText.

Definition at line 160 of file qstatictext.cpp.

161  : data(new QStaticTextPrivate)
162 {
163 }
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99

◆ QStaticText() [2/3]

QStaticText::QStaticText ( const QString text)

Constructs a QStaticText object with the given text.

Definition at line 168 of file qstatictext.cpp.

169  : data(new QStaticTextPrivate)
170 {
171  data->text = text;
172  data->invalidate();
173 }
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99
QString text() const
Returns the text of the QStaticText.

◆ QStaticText() [3/3]

QStaticText::QStaticText ( const QStaticText other)

Constructs a QStaticText object which is a copy of other.

Definition at line 178 of file qstatictext.cpp.

179 {
180  data = other.data;
181 }
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99

◆ ~QStaticText()

QStaticText::~QStaticText ( )

Destroys the QStaticText.

Definition at line 186 of file qstatictext.cpp.

187 {
188  Q_ASSERT(!data || data->ref >= 1);
189 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99

Functions

◆ detach()

void QStaticText::detach ( )
private
Warning
This function is not part of the public interface.

Definition at line 194 of file qstatictext.cpp.

Referenced by setPerformanceHint(), setText(), setTextFormat(), setTextOption(), and setTextWidth().

195 {
196  if (data->ref != 1)
197  data.detach();
198 }
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99
void detach()
If the shared data object&#39;s reference count is greater than 1, this function creates a deep copy of t...
Definition: qshareddata.h:148

◆ 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 249 of file qstatictext.cpp.

250 {
251  return !(*this == other);
252 }

◆ operator=()

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

Assigns other to this QStaticText.

Definition at line 227 of file qstatictext.cpp.

228 {
229  data = other.data;
230  return *this;
231 }
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99

◆ 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 237 of file qstatictext.cpp.

238 {
239  return (data == other.data
240  || (data->text == other.data->text
241  && data->font == other.data->font
242  && data->textWidth == other.data->textWidth));
243 }
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99

◆ performanceHint()

QStaticText::PerformanceHint QStaticText::performanceHint ( ) const

Returns which performance hint is set for the QStaticText.

See also
setPerformanceHint()

Definition at line 334 of file qstatictext.cpp.

335 {
337 }
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99
unsigned char useBackendOptimizations

◆ 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::setMatrix()

Definition at line 216 of file qstatictext.cpp.

217 {
218  data->matrix = matrix;
219  data->font = font;
220  data->init();
221 }
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99

◆ 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 318 of file qstatictext.cpp.

319 {
322  return;
323  }
324  detach();
326  data->invalidate();
327 }
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99
PerformanceHint performanceHint() const
Returns which performance hint is set for the QStaticText.
unsigned char useBackendOptimizations

◆ 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 261 of file qstatictext.cpp.

262 {
263  detach();
264  data->text = text;
265  data->invalidate();
266 }
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99
QString text() const
Returns the text of the QStaticText.

◆ 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 279 of file qstatictext.cpp.

280 {
281  detach();
283  data->invalidate();
284 }
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99
Qt::TextFormat textFormat() const
Returns the text format of the QStaticText.
unsigned char textFormat

◆ 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 344 of file qstatictext.cpp.

345 {
346  detach();
348  data->invalidate();
349 }
QTextOption textOption
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99
QTextOption textOption() const
Returns the current text option used to control the layout process.

◆ 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 372 of file qstatictext.cpp.

373 {
374  detach();
376  data->invalidate();
377 }
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99
qreal textWidth() const
Returns the preferred width for this QStaticText.

◆ size()

QSizeF QStaticText::size ( ) const

Returns the size of the bounding rect for this QStaticText.

See also
textWidth()

Definition at line 394 of file qstatictext.cpp.

395 {
396  if (data->needsRelayout)
397  data->init();
398  return data->actualSize;
399 }
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99
unsigned char needsRelayout

◆ text()

QString QStaticText::text ( ) const

Returns the text of the QStaticText.

See also
setText()

Definition at line 301 of file qstatictext.cpp.

Referenced by QPainter::drawStaticText(), QStaticText(), and setText().

302 {
303  return data->text;
304 }
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99

◆ textFormat()

Qt::TextFormat QStaticText::textFormat ( ) const

Returns the text format of the QStaticText.

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

Definition at line 291 of file qstatictext.cpp.

Referenced by setTextFormat().

292 {
293  return Qt::TextFormat(data->textFormat);
294 }
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99
TextFormat
Definition: qnamespace.h:1310
unsigned char textFormat

◆ textOption()

QTextOption QStaticText::textOption ( ) const

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

Definition at line 354 of file qstatictext.cpp.

Referenced by setTextOption().

355 {
356  return data->textOption;
357 }
QTextOption textOption
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99

◆ textWidth()

qreal QStaticText::textWidth ( ) const

Returns the preferred width for this QStaticText.

See also
setTextWidth()

Definition at line 384 of file qstatictext.cpp.

Referenced by setTextWidth().

385 {
386  return data->textWidth;
387 }
QExplicitlySharedDataPointer< QStaticTextPrivate > data
Definition: qstatictext.h:99

Friends and Related Functions

◆ QStaticTextPrivate

friend class QStaticTextPrivate
friend

Definition at line 100 of file qstatictext.h.

Properties

◆ data


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