Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QStaticText Class

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

Header:#include <QStaticText>
Since: Qt 4.7

Public Types

enumPerformanceHint { ModerateCaching, AggressiveCaching }

Public Functions

QStaticText()
QStaticText(const QString & text)
QStaticText(const QStaticText & other)
~QStaticText()
PerformanceHintperformanceHint() const
voidprepare(const QTransform & matrix = QTransform(), const QFont & font = QFont())
voidsetPerformanceHint(PerformanceHint performanceHint)
voidsetText(const QString & text)
voidsetTextFormat(Qt::TextFormat textFormat)
voidsetTextOption(const QTextOption & textOption)
voidsetTextWidth(qreal textWidth)
QSizeFsize() const
QStringtext() const
Qt::TextFormattextFormat() const
QTextOptiontextOption() const
qrealtextWidth() const
booloperator!=(const QStaticText & other) const
QStaticText &operator=(const QStaticText & other)
booloperator==(const QStaticText & other) const

Detailed Description

TheQStaticText 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 usingQPainter::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 usingQStaticText.

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:publicQWidget{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;};

TheQStaticText class can be used to mimic the behavior ofQPainter::drawText() to a specific point with no boundaries, and also whenQPainter::drawText() is called with a bounding rectangle.

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

If you set a text width on theQStaticText object, this will bound the text. The text will be formatted so that no line exceeds the given width. The text width set forQStaticText will not automatically be used for clipping. To achieve clipping in addition to line breaks, useQPainter::setClipRect(). The position of the text is decided by the argument passed toQPainter::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 byQTextDocument.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 forceQStaticText to display its contents as either plain text or rich text, use the functionQStaticText::setTextFormat() and pass in, respectively,Qt::PlainText andQt::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 theQPainter::drawStaticText() call where it occurs. To avoid this overhead in the paint event, you can callprepare() ahead of time to ensure that the layout is calculated.

See alsoQPainter::drawText(),QPainter::drawStaticText(),QTextLayout, andQTextDocument.

Member Type Documentation

enum QStaticText::PerformanceHint

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

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()

Constructs an emptyQStaticText

QStaticText::QStaticText(constQString & text)

Constructs aQStaticText object with the giventext.

QStaticText::QStaticText(constQStaticText & other)

Constructs aQStaticText object which is a copy ofother.

QStaticText::~QStaticText()

Destroys theQStaticText.

PerformanceHint QStaticText::performanceHint() const

Returns which performance hint is set for theQStaticText.

See alsosetPerformanceHint().

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

Prepares theQStaticText object for being painted with the givenmatrix and the givenfont to avoid overhead when the actual drawStaticText() call is made.

When drawStaticText() is called, the layout of theQStaticText will be recalculated if any part of theQStaticText 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 theQStaticText 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 theQStaticText after making changes, you can use the prepare() function and pass in thematrix andfont you expect to use when drawing the text.

See alsoQPainter::setFont() andQPainter::setMatrix().

void QStaticText::setPerformanceHint(PerformanceHint performanceHint)

Sets the performance hint of theQStaticText according to theperformanceHint provided. TheperformanceHint is used to customize how much caching is done internally to improve performance.

The default isQStaticText::ModerateCaching.

Note:This function will cause the layout of the text to require recalculation.

See alsoperformanceHint().

void QStaticText::setText(constQString & text)

Sets the text of theQStaticText totext.

Note:This function will cause the layout of the text to require recalculation.

See alsotext().

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

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

Note:This function will cause the layout of the text to require recalculation.

See alsotextFormat(),setText(), andtext().

void QStaticText::setTextOption(constQTextOption & textOption)

Sets the text option structure that controls the layout process to the giventextOption.

See alsotextOption().

void QStaticText::setTextWidth(qreal textWidth)

Sets the preferred width for thisQStaticText. 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 specifiedtextWidth.

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

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

Note:This function will cause the layout of the text to require recalculation.

See alsotextWidth() andsize().

QSizeF QStaticText::size() const

Returns the size of the bounding rect for thisQStaticText.

See alsotextWidth().

QString QStaticText::text() const

Returns the text of theQStaticText.

See alsosetText().

Qt::TextFormat QStaticText::textFormat() const

Returns the text format of theQStaticText.

See alsosetTextFormat(),setText(), andtext().

QTextOption QStaticText::textOption() const

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

See alsosetTextOption().

qreal QStaticText::textWidth() const

Returns the preferred width for thisQStaticText.

See alsosetTextWidth().

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

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

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

Assignsother to thisQStaticText.

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

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

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp