Skip to content

PythonQt.QtGui.QStaticText

QStaticText

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

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.

Source: https://doc.qt.io/archives/qt-5.15/qstatictext.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
AggressiveCaching attribute Any
ModerateCaching attribute Any
PerformanceHint attribute type Convert a number or string to an integer, or return 0 if no arguments are given.
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
operator_assign function (self, arg__1: Any) -> Any X.operator_assign(arg__1) -> PythonQt.QtGui.QStaticText
performanceHint function (self) -> Any Returns which performance hint is set for the [QStaticText](https://doc.qt.io/archives/qt-5.15/qstatictext.html).
prepare function (self, matrix: Any, font: Any) -> None X.prepare(matrix, font)
setPerformanceHint function (self, performanceHint: Any) -> None Sets the performance hint of the [QStaticText](https://doc.qt.io/archives/qt-5.15/qstatictext.html) according to the performanceHint provided.
setText function (self, text: str) -> None Sets the text of the [QStaticText](https://doc.qt.io/archives/qt-5.15/qstatictext.html) to text.
setTextFormat function (self, textFormat: Any) -> None Sets the text format of the [QStaticText](https://doc.qt.io/archives/qt-5.15/qstatictext.html) to textFormat.
setTextOption function (self, textOption: Any) -> None Sets the text option structure that controls the layout process to the given textOption.
setTextWidth function (self, textWidth: float) -> None Sets the preferred width for this [QStaticText](https://doc.qt.io/archives/qt-5.15/qstatictext.html).
size function (self) -> Any X.size() -> PythonQt.QtCore.QSizeF
swap function (self, other: Any) -> None Swaps this static text instance with other.
text function (self) -> str X.text() -> str
textFormat function (self) -> Any Returns the text format of the [QStaticText](https://doc.qt.io/archives/qt-5.15/qstatictext.html).
textOption function (self) -> Any X.textOption() -> PythonQt.QtGui.QTextOption
textWidth function (self) -> float X.textWidth() -> float