- Qt 6.10
- Qt Quick
- C++ Classes
- QSGTextNode
- Qt 6.10.2 Reference Documentation

# QSGTextNode Class

The QSGTextNode class is a class for drawing text layouts and text documents in the Qt Quick scene graph. More...

Header: | `#include <QSGTextNode>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Quick)` `target_link_libraries(mytarget PRIVATE Qt6::Quick)`
qmake: | `QT += quick`
Since: | Qt 6.7
Inherits: | QSGTransformNode

- List of all members, including inherited members

## Public Types

enum | RenderType { QtRendering, NativeRendering, CurveRendering }
enum | TextStyle { Normal, Outline, Raised, Sunken }

## Public Functions

void | addTextDocument(QPointF position, QTextDocument *document, int selectionStart = -1, int selectionCount = -1)
void | addTextLayout(QPointF position, QTextLayout *layout, int selectionStart = -1, int selectionCount = -1, int lineStart = 0, int lineCount = -1)
virtual void | clear() = 0
virtual QColor | color() const = 0
virtual QSGTexture::Filtering | filtering() const = 0
virtual QColor | linkColor() const = 0
virtual QSGTextNode::RenderType | renderType() const = 0
virtual int | renderTypeQuality() const = 0
virtual QColor | selectionColor() const = 0
virtual QColor | selectionTextColor() const = 0
virtual void | setColor(QColor color) = 0
virtual void | setFiltering(QSGTexture::Filtering filtering) = 0
virtual void | setLinkColor(QColor linkColor) = 0
virtual void | setRenderType(QSGTextNode::RenderType renderType) = 0
virtual void | setRenderTypeQuality(int renderTypeQuality) = 0
virtual void | setSelectionColor(QColor color) = 0
virtual void | setSelectionTextColor(QColor selectionTextColor) = 0
virtual void | setStyleColor(QColor styleColor) = 0
virtual void | setTextStyle(QSGTextNode::TextStyle textStyle) = 0
virtual void | setViewport(const QRectF &viewport) = 0
virtual QColor | styleColor() const = 0
virtual QSGTextNode::TextStyle | textStyle() = 0
virtual QRectF | viewport() const = 0

## Detailed Description

QSGTextNode can be useful for creating custom Qt Quick items that require text. It is used in Qt Quick by the Text, TextEdit and TextInput elements.

You can create QSGTextNode objects using QQuickWindow::createTextNode(). The addTextLayout() and addTextDocument() functions provide ways to add text to the QSGTextNode. The text must already be laid out.

Note: Properties must be set before addTextLayout() or addTextDocument() are called in order to have an effect.

Note: The destruction of QSGTextNode has to be managed with care. In particular, since it references graphics resources, it must be deleted when the Qt Quick scene graph is invalidated. If the node is part of the graph and has the `OwnedByParent` flag set (which is the default), this will happen automatically. However, if the `OwnedByParent` flag is cleared and the node is disposed of manually, care must be taken to do this when the scene graph is invalidated. This can be done by connecting to the QQuickWindow::sceneGraphInvalidated() signal, or by implementing a slot in the QQuickItem subclass which is named `invalidateSceneGraph()`. See also the documentation of QQuickItem for more details.

## Member Type Documentation

### enum QSGTextNode::RenderType

This enum type describes type of glyph node used for rendering the text.

Constant | Value | Description
--- | --- | ---
`QSGTextNode::QtRendering` | `0` | Text is rendered using a scalable distance field for each glyph.
`QSGTextNode::NativeRendering` | `1` | Text is rendered using a platform-specific technique.
`QSGTextNode::CurveRendering` | `2` | Text is rendered using a curve rasterizer running directly on the graphics hardware.

Select `NativeRendering` if you prefer text to look native on the target platform and do not require advanced features such as transformation of the text. Using such features in combination with the NativeRendering render type will lend poor and sometimes pixelated results.

Both `Text.QtRendering` and `Text.CurveRendering` are hardware-accelerated techniques. `QtRendering` is the faster of the two, but uses more memory and will exhibit rendering artifacts at large sizes. `CurveRendering` should be considered as an alternative in cases where `QtRendering` does not give good visual results or where reducing graphics memory consumption is a priority.

See also setRenderType() and setRenderTypeQuality().

### enum QSGTextNode::TextStyle

This enum type describes styles that can be applied to text rendering.

Constant | Value | Description
--- | --- | ---
`QSGTextNode::Normal` | `0` | The text is drawn without any style applied.
`QSGTextNode::Outline` | `1` | The text is drawn with an outline.
`QSGTextNode::Raised` | `2` | The text is drawn raised.
`QSGTextNode::Sunken` | `3` | The text is drawn sunken.

See also setTextStyle() and setStyleColor().

## Member Function Documentation

### void QSGTextNode::addTextDocument(QPointF position, QTextDocument *document, int selectionStart = -1, int selectionCount = -1)

Adds the contents of document to the text node at position. If selectionStart is >= 0, then this marks the first character in a selected area of selectionCount number of characters. The selection is represented as a background fill with the selectionColor() and the selected text is rendered in the selectionTextColor().

This function forwards its arguments to the virtual function doAddTextDocument().

See also clear().

### void QSGTextNode::addTextLayout(QPointF position, QTextLayout *layout, int selectionStart = -1, int selectionCount = -1, int lineStart = 0, int lineCount = -1)

Adds the contents of layout to the text node at position. If selectionStart is >= 0, then this marks the first character in a selected area of selectionCount number of characters. The selection is represented as a background fill with the selectionColor() and the selected text is rendered in the selectionTextColor().

For convenience, lineStart and lineCount can be used to select the range of QTextLine objects to include from the layout. This can be useful, for instance, when creating elided layouts. If lineCount is < 0, then the the node will include the lines from lineStart to the end of the layout.

This function forwards its arguments to the virtual function doAddTextLayout().

See also clear().

### `[pure virtual]` void QSGTextNode::clear()

Clears the contents of the node, deleting nodes and other data that represents the layouts and documents that have been added to it.

See also addTextLayout() and addTextDocument().

### `[pure virtual]` QColor QSGTextNode::color() const

Returns the main color used when rendering the text.

See also setColor().

### `[pure virtual]` QSGTexture::Filtering QSGTextNode::filtering() const

Returns the sampling mode used when scaling images that are part of the displayed text.

See also setFiltering().

### `[pure virtual]` QColor QSGTextNode::linkColor() const

Returns the color of hyperlinks in the text.

See also setLinkColor().

### `[pure virtual]` QSGTextNode::RenderType QSGTextNode::renderType() const

Returns the type of glyph node used for rendering the text.

See also setRenderType().

### `[pure virtual]` int QSGTextNode::renderTypeQuality() const

Returns the render type quality of the node. See setRenderTypeQuality() for details.

See also setRenderTypeQuality().

### `[pure virtual]` QColor QSGTextNode::selectionColor() const

Returns the color of the selection background when any part of the text is marked as selected.

See also setSelectionColor().

### `[pure virtual]` QColor QSGTextNode::selectionTextColor() const

Returns the color of the selection text when any part of the text is marked as selected.

See also setSelectionTextColor().

### `[pure virtual]` void QSGTextNode::setColor(QColor color)

Sets the main color to use when rendering the text to color.

The default is black: `QColor(0, 0, 0)`.

See also color().

### `[pure virtual]` void QSGTextNode::setFiltering(QSGTexture::Filtering filtering)

Sets the sampling mode used when scaling images that are part of the displayed text to filtering. For smoothly scaled images, use QSGTexture::Linear here.

The default is QSGTexture::Nearest.

See also filtering().

### `[pure virtual]` void QSGTextNode::setLinkColor(QColor linkColor)

Sets the color of or hyperlinks to linkColor in the text.

The default is blue: `QColor(0, 0, 255)`.

See also linkColor().

### `[pure virtual]` void QSGTextNode::setRenderType(QSGTextNode::RenderType renderType)

Sets the type of glyph node in use to renderType.

The default is QtRendering.

See also renderType().

### `[pure virtual]` void QSGTextNode::setRenderTypeQuality(int renderTypeQuality)

If the renderType() in use supports it, set the quality to use when rendering the text. When supported, this can be used to trade visual fidelity for execution speed or memory.

When the renderTypeQuality is < 0, the default quality is used.

The renderTypeQuality can be any integer, although limitations imposed by the underlying graphics hardware may be encountered if extreme values are set. The Qt Quick Text element operates with the following predefined values:

Constant | Description
--- | ---
`DefaultRenderTypeQuality` | -1 (default)
`LowRenderTypeQuality` | 26
`NormalRenderTypeQuality` | 52
`HighRenderTypeQuality` | 104
`VeryHighRenderTypeQuality` | 208

This value is currently only respected by the QtRendering render type. Setting it changes the resolution of the distance fields used to represent the glyphs. Setting it above normal will cause memory consumption to increase, but reduces filtering artifacts on very large text.

The default is -1.

See also renderTypeQuality().

### `[pure virtual]` void QSGTextNode::setSelectionColor(QColor color)

Sets the color of the selection background to color when any part of the text is marked as selected.

The default is dark blue: `QColor(0, 0, 128)`.

See also selectionColor().

### `[pure virtual]` void QSGTextNode::setSelectionTextColor(QColor selectionTextColor)

Sets the color of the selection text to selectionTextColor when any part of the text is marked as selected.

The default is white: `QColor(255, 255, 255)`.

See also selectionTextColor().

### `[pure virtual]` void QSGTextNode::setStyleColor(QColor styleColor)

Sets the style color to use when rendering the text to styleColor.

The default is black: `QColor(0, 0, 0)`.

See also styleColor() and setTextStyle().

### `[pure virtual]` void QSGTextNode::setTextStyle(QSGTextNode::TextStyle textStyle)

Sets the style of the rendered text to textStyle. The default is `Normal`.

See also textStyle() and setStyleColor().

### `[pure virtual]` void QSGTextNode::setViewport(const QRectF &viewport)

Sets the bounding rect of the viewport where the text is displayed to viewport. Providing this information makes it possible for the QSGTextNode to optimize which parts of the text layout or document are included in the scene graph.

The default is a default-constructed QRectF. For this viewport, all contents will be included in the graph.

See also viewport().

### `[pure virtual]` QColor QSGTextNode::styleColor() const

Returns the style color used when rendering the text.

See also setStyleColor() and textStyle().

### `[pure virtual]` QSGTextNode::TextStyle QSGTextNode::textStyle()

Returns the style of the rendered text.

See also setTextStyle() and styleColor().

### `[pure virtual]` QRectF QSGTextNode::viewport() const

Returns the current viewport set for this QSGTextNode.

See also setViewport().