QSGTextNode Class
QSGTextNode 类是一个用于在Qt Quick 场景图中绘制文本布局和文本文档的类。更多
Header: | #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 |
详细说明
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() 和 doAddTextDocument()。
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() 和 doAddTextLayout()。
[pure virtual]
void QSGTextNode::clear()
清除节点的内容,删除节点和其他表示已添加到节点中的布局和文档的数据。
另请参阅 addTextLayout() 和addTextDocument()。
[pure virtual]
QColor QSGTextNode::color() const
返回渲染文本时使用的主色调。
另请参阅 setColor()。
[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 文本元素使用以下预定义值运行:
常量 | 说明 |
---|---|
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() 。
© 2025 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.