QSGTextNode Class
QSGTextNode 类是一个用于在Qt Quick 场景图中绘制文本布局和文本文档的类。更多内容...
| 头文件: | #include <QSGTextNode> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Quick) target_link_libraries(mytarget PRIVATE Qt6::Quick) |
| qmake: | QT += quick |
| 自: | Qt 6.7 |
| 继承自: | QSGTransformNode |
公共类型
| enum | RenderType { QtRendering, NativeRendering, CurveRendering } |
| enum | TextStyle { Normal, Outline, Raised, Sunken } |
公共函数
| 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 |
私有函数
| virtual void | doAddTextDocument(QPointF position, QTextDocument *document, int selectionStart, int selectionCount) = 0 |
| virtual void | doAddTextLayout(QPointF position, QTextLayout *layout, int selectionStart, int selectionCount, int lineStart, int lineCount) = 0 |
详细说明
QSGTextNode可用于创建需要文本的自定义Qt Quick 项。它在Qt Quick 中由Text、TextEdit 和TextInput 元素使用。
您可以使用 `QQuickWindow::createTextNode()` 创建 `QSGTextNode` 对象。函数 `addTextLayout()` 和 `addTextDocument()` 提供了向 `QSGTextNode` 添加文本的方法。该文本必须已排版完成。
注意: 为了使属性 生效,必须在调用addTextLayout() 或addTextDocument() 之前设置这些属性 。
注意: 必须谨慎管理 QSGTextNode 的销毁 。特别是,由于它引用了图形资源,因此在Qt Quick 场景图失效时必须将其删除。如果该节点是场景图的一部分且已设置OwnedByParent 标志(这是默认设置),则会自动执行此操作。 但是,如果OwnedByParent 标志被清除且节点被手动销毁,则必须注意在场景图失效时执行此操作。可以通过连接到QQuickWindow::sceneGraphInvalidated()信号,或在QQuickItem 子类中实现名为invalidateSceneGraph() 的槽来实现。更多详细信息请参阅QQuickItem 的文档。
成员类型文档
enum QSGTextNode::RenderType
此枚举类型描述了用于渲染文本的字形节点的类型。
| 常量 | 值 | 描述 |
|---|---|---|
QSGTextNode::QtRendering | 0 | 文本通过为每个字形使用可缩放的距离场进行渲染。 |
QSGTextNode::NativeRendering | 1 | 文本使用特定于平台的技术进行渲染。 |
QSGTextNode::CurveRendering | 2 | 文本使用直接在图形硬件上运行的曲线光栅化器进行渲染。 |
如果您希望文本在目标平台上呈现原生外观,且不需要文本变换等高级功能,请选择“NativeRendering ”。在“NativeRendering”渲染类型下使用此类功能,会导致效果不佳,有时甚至会出现像素化现象。
Text.QtRendering 和Text.CurveRendering 都是硬件加速技术。QtRendering 速度更快,但占用更多内存,且在大尺寸下会出现渲染瑕疵。当QtRendering 无法提供良好的视觉效果,或者优先考虑降低图形内存消耗时,应考虑将CurveRendering 作为替代方案。
另请参阅 setRenderType() 和setRenderTypeQuality()。
enum QSGTextNode::TextStyle
此枚举类型描述了可应用于文本渲染的样式。
| 常量 | 值 | 描述 |
|---|---|---|
QSGTextNode::Normal | 0 | 文本在未应用任何样式的情况下绘制。 |
QSGTextNode::Outline | 1 | 文本以带轮廓的形式绘制。 |
QSGTextNode::Raised | 2 | 文本以凸起效果绘制。 |
QSGTextNode::Sunken | 3 | 文本以凹陷样式绘制。 |
另请参阅 setTextStyle() 和setStyleColor()。
成员函数文档
void QSGTextNode::addTextDocument(QPointF position, QTextDocument *document, int selectionStart = -1, int selectionCount = -1)
将document 的内容添加到位于position 的文本节点中。如果selectionStart ≥0,则这将标记为selectionCount 个字符所选区域中的第一个字符。该选区通过selectionColor()表示为背景填充,所选文本则通过selectionTextColor()进行渲染。
该函数将其参数转发给虚函数doAddTextDocument()。
另请参阅 clear()。
void QSGTextNode::addTextLayout(QPointF position, QTextLayout *layout, int selectionStart = -1, int selectionCount = -1, int lineStart = 0, int lineCount = -1)
将layout 的内容添加到position 处的文本节点中。如果selectionStart >= 0,则这将标记为一个包含selectionCount 个字符的选定区域中的第一个字符。该选定区域通过selectionColor() 表示为背景填充,而选定的文本则通过selectionTextColor() 进行渲染。
为方便起见,可使用lineStart 和lineCount 来选择要从布局中包含的QTextLine 对象范围。例如,在创建省略布局时,此功能会非常有用。如果lineCount < 0,则该节点将包含从lineStart 到布局末尾的行。
该函数将其参数转发给虚函数doAddTextLayout()。
另请参阅 clear()。
[pure virtual] void QSGTextNode::clear()
清除节点的内容,删除已添加到该节点的表示布局和文档的节点及其他数据。
另请参阅 addTextLayout() 和addTextDocument()。
[pure virtual] QColor QSGTextNode::color() const
返回渲染文本时使用的主颜色。
另请参阅 ` setColor()`。
[pure virtual private] void QSGTextNode::doAddTextDocument(QPointF position, QTextDocument *document, int selectionStart, int selectionCount)
由addTextDocument()调用的虚拟函数,该函数将document 中的内容转换为场景图节点,并将其添加到position 处的当前节点中。
如果selectionStart >= 0,则这表示selectionCount 中选定区域(包含 数量的字符)的第一个字符。选定区域通过selectionColor() 表示为背景填充,而选定的文本则在selectionTextColor() 中渲染。
另请参阅 clear() 和addTextDocument()。
[pure virtual private] void QSGTextNode::doAddTextLayout(QPointF position, QTextLayout *layout, int selectionStart, int selectionCount, int lineStart, int lineCount)
由addTextLayout()调用的虚拟函数,该函数将layout 中的内容转换为场景图节点,并将其添加到position 处的当前节点上。
如果selectionStart >= 0,则这表示selectionCount 中选定区域(包含 个字符)的第一个字符。选定区域通过selectionColor() 函数以背景填充的形式呈现,而选定文本则通过selectionTextColor() 函数进行渲染。
为方便起见,可使用lineStart 和lineCount 来选择要从布局中包含的QTextLine 对象范围。例如,在创建省略布局时,这会非常有用。如果lineCount < 0,则该节点将包含从lineStart 到布局末尾的行。
另请参阅 clear() 和addTextLayout()。
[pure virtual] QSGTexture::Filtering QSGTextNode::filtering() const
返回对作为显示文本组成部分的图像进行缩放时所使用的采样模式。
另请参阅 setFiltering()。
[pure virtual] QColor QSGTextNode::linkColor() const
返回文本中超链接的颜色。
另请参阅 setLinkColor()。
[pure virtual] QSGTextNode::RenderType QSGTextNode::renderType() const
返回用于渲染文本的字形节点的类型。
另请参阅 setRenderType()。
[pure virtual] int QSGTextNode::renderTypeQuality() const
返回节点的渲染类型质量。详情请参阅setRenderTypeQuality()。
另请参阅 setRenderTypeQuality()。
[pure virtual] QColor QSGTextNode::selectionColor() const
当文本的任何部分被标记为选中时,返回选中区域背景的颜色。
另请参阅 setSelectionColor()。
[pure virtual] QColor QSGTextNode::selectionTextColor() const
当文本的任何部分被标记为选中时,返回选中文本的颜色。
另请参阅 setSelectionTextColor()。
[pure virtual] void QSGTextNode::setColor(QColor color)
将渲染文本时使用的主颜色设置为color 。
默认颜色为黑色:QColor(0, 0, 0) 。
另请参阅 color()。
[pure virtual] void QSGTextNode::setFiltering(QSGTexture::Filtering filtering)
将显示文本中包含的图像进行缩放时使用的采样模式设置为filtering 。若要获得平滑缩放的图像,请在此处使用QSGTexture::Linear 。
默认值为QSGTexture::Nearest 。
另请参阅 filtering()。
[pure virtual] void QSGTextNode::setLinkColor(QColor linkColor)
设置文本中超链接的颜色,或设置文本链接指向linkColor 时的颜色。
默认颜色为蓝色:QColor(0, 0, 255) 。
另请参阅 linkColor()。
[pure virtual] void QSGTextNode::setRenderType(QSGTextNode::RenderType renderType)
将当前使用的字形节点的类型设置为renderType 。
默认值为QtRendering 。
另请参阅 renderType()。
[pure virtual] void QSGTextNode::setRenderTypeQuality(int renderTypeQuality)
如果正在使用的renderType() 支持此功能,请设置渲染文本时使用的质量。在支持的情况下,可通过此设置在视觉保真度与执行速度或内存占用之间进行权衡。
当renderTypeQuality 的值小于 0 时,将使用默认质量。
renderTypeQuality 可以是任意整数,但如果设置为极端值,可能会受到底层图形硬件的限制。Qt Quick 的 Text 元素支持以下预定义值:
| 常量 | 描述 |
|---|---|
DefaultRenderTypeQuality | -1(默认) |
LowRenderTypeQuality | 26 |
NormalRenderTypeQuality | 52 |
HighRenderTypeQuality | 104 |
VeryHighRenderTypeQuality | 208 |
目前仅“QtRendering ”渲染类型会遵循此值。设置此值会改变用于表示字形的距离字段的分辨率。将其设置为高于正常值会导致内存消耗增加,但可减少超大文本中的滤波伪影。
默认值为 -1。
另请参阅 renderTypeQuality()。
[pure virtual] void QSGTextNode::setSelectionColor(QColor color)
当文本的任何部分被选中时,将选中区域的背景色设置为color 。
默认颜色为深蓝色:QColor(0, 0, 128) 。
另请参阅 selectionColor()。
[pure virtual] void QSGTextNode::setSelectionTextColor(QColor selectionTextColor)
当文本的任何部分被选中时,将选中文本的颜色设置为selectionTextColor 。
默认颜色为白色:QColor(255, 255, 255) 。
另请参阅 selectionTextColor()。
[pure virtual] void QSGTextNode::setStyleColor(QColor styleColor)
设置将文本渲染为styleColor 时使用的样式颜色。
默认颜色为黑色:QColor(0, 0, 0) 。
另请参阅 styleColor() 和setTextStyle()。
[pure virtual] void QSGTextNode::setTextStyle(QSGTextNode::TextStyle textStyle)
将渲染文本的样式设置为textStyle 。默认样式为Normal 。
另请参阅 textStyle() 和setStyleColor()。
[pure virtual] void QSGTextNode::setViewport(const QRectF &viewport)
将显示文本的视口边界矩形设置为viewport 。提供此信息可使QSGTextNode 能够优化文本布局或文档中哪些部分应被纳入场景图。
默认值为通过默认构造函数构造的QRectF 。对于此视口,所有内容都将被纳入场景图。
另请参阅 viewport()。
[pure virtual] QColor QSGTextNode::styleColor() const
返回渲染文本时使用的样式颜色。
另请参阅 setStyleColor() 和textStyle()。
[pure virtual] QSGTextNode::TextStyle QSGTextNode::textStyle()
返回渲染后文本的样式。
另请参阅 setTextStyle() 和styleColor()。
[pure virtual] QRectF QSGTextNode::viewport() const
返回为该QSGTextNode 设置的当前视口。
另请参阅 setViewport()。
© 2026 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 the GNU 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.