本页内容

QSGGeometry Class

QSGGeometry 类为Qt Quick 场景图中的图形基元提供了低级存储功能。更多内容...

头文件: #include <QSGGeometry>
CMake: find_package(Qt6 REQUIRED COMPONENTS Quick)
target_link_libraries(mytarget PRIVATE Qt6::Quick)
qmake: QT += quick

公共类型

struct Attribute
struct AttributeSet
struct ColoredPoint2D
struct Point2D
struct TexturedPoint2D
enum AttributeType { UnknownAttribute, PositionAttribute, ColorAttribute, TexCoordAttribute, TexCoord1Attribute, TexCoord2Attribute }
enum DataPattern { AlwaysUploadPattern, DynamicPattern, StaticPattern, StreamPattern }
enum DrawingMode { DrawPoints, DrawLines, DrawLineStrip, DrawTriangles, DrawTriangleStrip }
enum Type { ByteType, UnsignedByteType, ShortType, UnsignedShortType, IntType, …, DoubleType }

公共函数

QSGGeometry(const QSGGeometry::AttributeSet &attributes, int vertexCount, int indexCount = 0, int indexType = UnsignedShortType)
virtual ~QSGGeometry()
void allocate(int vertexCount, int indexCount = 0)
int attributeCount() const
const QSGGeometry::Attribute *attributes() const
unsigned int drawingMode() const
int indexCount() const
void *indexData()
const void *indexData() const
uint *indexDataAsUInt()
const uint *indexDataAsUInt() const
quint16 *indexDataAsUShort()
const quint16 *indexDataAsUShort() const
QSGGeometry::DataPattern indexDataPattern() const
int indexType() const
float lineWidth() const
void markIndexDataDirty()
void markVertexDataDirty()
void setDrawingMode(unsigned int mode)
(since 6.10) void setIndexCount(int count)
void setIndexDataPattern(QSGGeometry::DataPattern p)
void setLineWidth(float width)
(since 6.10) void setVertexCount(int count)
void setVertexDataPattern(QSGGeometry::DataPattern p)
int sizeOfIndex() const
int sizeOfVertex() const
int vertexCount() const
void *vertexData()
const void *vertexData() const
QSGGeometry::ColoredPoint2D *vertexDataAsColoredPoint2D()
const QSGGeometry::ColoredPoint2D *vertexDataAsColoredPoint2D() const
QSGGeometry::Point2D *vertexDataAsPoint2D()
const QSGGeometry::Point2D *vertexDataAsPoint2D() const
QSGGeometry::TexturedPoint2D *vertexDataAsTexturedPoint2D()
const QSGGeometry::TexturedPoint2D *vertexDataAsTexturedPoint2D() const
QSGGeometry::DataPattern vertexDataPattern() const

静态公共成员

const QSGGeometry::AttributeSet &defaultAttributes_ColoredPoint2D()
const QSGGeometry::AttributeSet &defaultAttributes_Point2D()
const QSGGeometry::AttributeSet &defaultAttributes_TexturedPoint2D()
void updateColoredRectGeometry(QSGGeometry *g, const QRectF &rect)
void updateRectGeometry(QSGGeometry *g, const QRectF &rect)
void updateTexturedRectGeometry(QSGGeometry *g, const QRectF &rect, const QRectF &textureRect)

详细说明

QSGGeometry 类用于存储通过场景图渲染的基元几何体。它包含顶点数据,并可选地包含索引数据。用于绘制几何体的模式(也称为基元拓扑)通过 `setDrawingMode()` 进行指定。

顶点可以简单到仅由 x 和 y 值定义的点,也可以更为复杂,即每个顶点包含法线、纹理坐标和 3D 位置。QSGGeometry::AttributeSet 用于描述顶点数据的构成方式。 属性集只能在构造时指定。QSGGeometry类默认提供了一些便捷属性及属性集。defaultAttributes_Point2D()函数返回用于普通纯色矩形的属性集,而defaultAttributes_TexturedPoint2D 函数则返回用于带纹理的2D几何体的属性。 顶点数据在内部以void * 格式存储,可通过vertexData()函数访问。常见属性集的便捷访问器可通过vertexDataAsPoint2D()和vertexDataAsTexturedPoint2D()获取。顶点数据的分配可通过向构造函数传递顶点计数,或在后续调用allocate()来实现。

构造完成后,可通过调用allocate() 方法调整数据缓冲区大小,从而更改顶点和索引的数量。但allocate() 方法每次调用时都需要更新所有顶点和索引数据。 自 Qt 6.10 起,setVertexCount() 和setIndexCount() 允许在不重新分配数据缓冲区的情况下调整顶点或索引的数量,且仅需更新新的顶点或索引。无论哪种情况,调用方都必须通过调用node->markDirty(QSGNode::DirtyGeometry) 将几何体节点标记为“已修改”,以确保渲染器有机会更新内部缓冲区。

QSGGeometry 可选地包含无符号 32 位、无符号 16 位或无符号 8 位整数的索引。索引类型必须在构造时指定,且无法更改。

以下代码片段演示了如何构建由位置顶点和颜色顶点组成的几何体。

struct MyPoint2D {
    float x;
    float y;
    float r;
    float g;
    float b;
    float a;

    void set(float x_, float y_, float r_, float g_, float b_, float a_) {
        x = x_;
        y = y_;
        r = r_;
        g = g_;
        b = b_;
        a = a_;
    }
};

QSGGeometry::Attribute MyPoint2D_Attributes[] = {
    QSGGeometry::Attribute::create(0, 2, FloatType, true),
    QSGGeometry::Attribute::create(1, 4, FloatType, false)
};

QSGGeometry::AttributeSet MyPoint2D_AttributeSet = {
    2,
    sizeof(MyPoint2D),
    MyPoint2D_Attributes
};

...

geometry = new QSGGeometry(MyPoint2D_AttributeSet, 2);
geometry->setDrawingMode(DrawLines);

MyPoint2D *vertices = static_cast<MyPoint2D *>(geometry->vertexData());
vertices[0].set(0, 0, 1, 0, 0, 1);
vertices[1].set(width(), height(), 0, 0, 1, 1);

就加速渲染而言,QSGGeometry 是一个软件缓冲区且位于客户端,因为 2D 图形中使用的缓冲区通常由许多小缓冲区组成,这些缓冲区每帧都会发生变化,因此无法从上传到图形内存中获益。 不过,QSGGeometry 支持通过 `setVertexDataPattern()` 和 `setIndexDataPattern()` 函数向渲染器提示应将缓冲区上传。该提示是否被采纳取决于具体实现。

注意:所有 以 QSG 为前缀的类均应仅在场景图的渲染线程上使用。有关更多信息,请参阅《场景图与渲染》。

另请参阅 QSGGeometryNode 和场景图 - 自定义几何体。

成员类型文档

enum QSGGeometry::AttributeType

此枚举定义了多种属性类型。

常量值描述
QSGGeometry::UnknownAttribute0无关紧要
QSGGeometry::PositionAttribute1位置
QSGGeometry::ColorAttribute2颜色
QSGGeometry::TexCoordAttribute3纹理坐标
QSGGeometry::TexCoord1Attribute4纹理坐标 1
QSGGeometry::TexCoord2Attribute5纹理坐标 2

enum QSGGeometry::DataPattern

DataPattern 枚举用于指定几何体对象中顶点和索引数据的使用模式。

常量值描述
QSGGeometry::AlwaysUploadPattern0数据始终会被上传。这意味着用户在修改索引和顶点数据后,无需显式将其标记为“已修改”。这是默认行为。
QSGGeometry::DynamicPattern2数据会被反复修改并多次绘制。这是一个提示,可能有助于提高性能。设置此选项时,用户必须确保在修改数据后将其标记为“已修改”。
QSGGeometry::StaticPattern3数据仅修改一次,但会被多次渲染。这是一个可能提升性能的提示。设置此选项时,用户必须确保在修改数据后将其标记为“已修改”。
QSGGeometry::StreamPattern1数据几乎在每次绘制时都会被修改。此提示可能有助于提升性能。启用此选项时,用户必须确保在修改数据后将其标记为“已修改”。

enum QSGGeometry::DrawingMode

指定绘制模式,也称为基元拓扑。

注意: 从 Qt 6开始, 场景图仅提供所有受支持的 3D 图形 API 都支持的拓扑结构。因此,尽管这些枚举值本身仍然存在,但在 Qt 6 的运行时环境中,DrawLineLoop 和DrawTriangleFan 这两个值不再受支持。

常量值
QSGGeometry::DrawPoints0x0000
QSGGeometry::DrawLines0x0001
QSGGeometry::DrawLineStrip0x0003
QSGGeometry::DrawTriangles0x0004
QSGGeometry::DrawTriangleStrip0x0005

enum QSGGeometry::Type

指定顶点数据中的组件类型。

常量值描述
QSGGeometry::ByteType0x1400 
QSGGeometry::UnsignedByteType0x1401 
QSGGeometry::ShortType0x1402 
QSGGeometry::UnsignedShortType0x1403 
QSGGeometry::IntType0x1404 
QSGGeometry::UnsignedIntType0x1405 
QSGGeometry::FloatType0x1406 
QSGGeometry::Bytes2Type0x1407新增于 Qt 5.14。
QSGGeometry::Bytes3Type0x1408新增于 Qt 5.14。
QSGGeometry::Bytes4Type0x1409新增于 Qt 5.14。
QSGGeometry::DoubleType0x140A新增于 Qt 5.14。

成员函数文档

QSGGeometry::QSGGeometry(const QSGGeometry::AttributeSet &attributes, int vertexCount, int indexCount = 0, int indexType = UnsignedShortType)

根据attributes 构建一个几何对象。

该对象会根据attributes 中累积的大小为vertexCount 的顶点分配空间,并为indexCount 分配空间。

indexType 可以是UnsignedShortType 或UnsignedIntType 。对后者的支持取决于运行时使用的图形API实现,并且可能并非总是可用。

几何对象默认以DrawTriangleStrip 作为绘制模式进行构建。

注意: attributes 及其引用的Attribute 对象在 QSGGeometry 的整个生命周期内必须保持有效。QSGGeometry 存储对attributes 的引用,但不会删除Attribute 对象。

[virtual noexcept] QSGGeometry::~QSGGeometry()

销毁该几何体对象及其已分配的顶点和索引数据。

void QSGGeometry::allocate(int vertexCount, int indexCount = 0)

将该几何对象的顶点和索引数据调整大小,使其适应vertexCount 个顶点和indexCount 个索引,并据此设置顶点和索引的数量。

使用setVertexCount() 或setIndexCount() 可在无需再次调用 allocate() 的情况下更改顶点或索引的数量。

此调用后,顶点和索引数据将失效,调用方必须通过调用node->markDirty(QSGNode::DirtyGeometry) 将相关的几何节点标记为“已修改”,以确保渲染器有机会更新内部缓冲区。

另请参阅 setVertexCount() 和setIndexCount()。

int QSGGeometry::attributeCount() const

返回此几何体所使用的属性集中包含的属性数量。

const QSGGeometry::Attribute *QSGGeometry::attributes() const

返回一个包含该几何体属性的数组。数组的大小由attributeCount() 指定。

[static] const QSGGeometry::AttributeSet &QSGGeometry::defaultAttributes_ColoredPoint2D()

一个便利函数,用于返回用于按顶点着色的二维绘图所需的属性。

[static] const QSGGeometry::AttributeSet &QSGGeometry::defaultAttributes_Point2D()

一个便捷函数,用于返回用于绘制二维纯色图形的属性。

[static] const QSGGeometry::AttributeSet &QSGGeometry::defaultAttributes_TexturedPoint2D()

一个便捷函数,用于返回用于带纹理的二维绘图的属性。

unsigned int QSGGeometry::drawingMode() const

返回该几何体的绘制模式。

默认值为DrawTriangleStrip 。

另请参阅 setDrawingMode()。

int QSGGeometry::indexCount() const

返回在渲染几何体对象时处理的索引数量。

另请参阅 setIndexCount()。

void *QSGGeometry::indexData()

返回指向该几何对象原始索引数据的指针。

另请参见 indexDataAsUShort() 和indexDataAsUInt()。

const void *QSGGeometry::indexData() const

返回指向该几何体对象原始索引数据的指针。

另请参阅 indexDataAsUShort() 和indexDataAsUInt()。

uint *QSGGeometry::indexDataAsUInt()

一种便捷函数,用于将索引数据作为由 32 位无符号整数组成的可变数组进行访问。

const uint *QSGGeometry::indexDataAsUInt() const

一种便捷函数,用于将索引数据作为由 32 位无符号整数组成的不可变数组进行访问。

quint16 *QSGGeometry::indexDataAsUShort()

一种便捷函数,用于将索引数据作为由 16 位无符号整数组成的可变数组进行访问。

const quint16 *QSGGeometry::indexDataAsUShort() const

一种便捷函数,用于将索引数据作为由 16 位无符号整数组成的不可变数组进行访问。

QSGGeometry::DataPattern QSGGeometry::indexDataPattern() const

返回该几何体中索引的使用模式。默认模式为AlwaysUploadPattern 。

另请参阅 setIndexDataPattern()。

int QSGGeometry::indexType() const

返回此几何体对象中用于索引的原始类型。

float QSGGeometry::lineWidth() const

获取当前线宽或点大小,或用于该几何体的线宽或点大小。当drawingMode 设置为DrawLines 或DrawLineStrip 时,此属性仅适用于线宽。在受支持的情况下,当drawingMode 设置为DrawPoints 时,此属性也适用于点大小。

默认值为1.0

注意: 点和线条绘制的支持 在运行时可能会受到限制,具体取决于平台和图形 API。例如,某些 API 不支持点精灵,因此无法设置 1 以外的大小。

注意: 1.0 的宽度始终受支持。

另请参阅 setLineWidth() 和drawingMode()。

void QSGGeometry::markIndexDataDirty()

标记该几何体的顶点已发生变化,必须重新上传。

此函数仅在顶点的使用模式为 StaticData 且渲染该几何体的渲染器将该几何体上传到顶点缓冲对象 (VBOs) 时才生效。

void QSGGeometry::markVertexDataDirty()

标记该几何体的顶点已发生变化,必须重新上传。

此函数仅在顶点的使用模式为 StaticData 且渲染该几何体的渲染器将几何体上传至顶点缓冲对象 (VBOs) 时才生效。

void QSGGeometry::setDrawingMode(unsigned int mode)

设置用于绘制此几何体的mode 。

默认值为QSGGeometry::DrawTriangleStrip 。

另请参阅 drawingMode() 和DrawingMode 。

[since 6.10] void QSGGeometry::setIndexCount(int count)

设置每次渲染几何体对象时要处理的索引数量。

count 不会经过有效性验证,用户有责任确保仅指定 0 到已分配索引数量之间的值。

调用此函数后,顶点和索引数据不会被置为无效,但调用方必须通过调用 `node->markDirty(QSGNode::DirtyGeometry)` 将几何节点标记为“已修改”,以确保渲染器有机会更新内部缓冲区。

该函数在 Qt 6.10 中引入。

另请参阅 indexCount()。

void QSGGeometry::setIndexDataPattern(QSGGeometry::DataPattern p)

将索引的使用模式设置为p 。

默认值为AlwaysUploadPattern 。当设置为默认值以外的任何值时,用户在修改索引数据后,除了需使用QSGNode::DirtyGeometry 调用QSGNode::markDirty()外,还必须调用markIndexDataDirty()。

另请参阅 indexDataPattern()。

void QSGGeometry::setLineWidth(float width)

将此几何体使用的线宽或点宽设置为width 。当drawingMode 为DrawLines 或DrawLineStrip 时,此属性仅适用于线宽。在受支持的情况下,当drawingMode 为DrawPoints 时,此属性也适用于点大小。

注意: 点和线绘制的支持 在运行时可能会受到限制,具体取决于平台和图形 API。例如,某些 API 不支持点精灵,因此无法设置除 1 以外的大小。

注意: 1.0 的宽度始终受支持。

另请参阅 lineWidth() 和drawingMode()。

[since 6.10] void QSGGeometry::setVertexCount(int count)

设置要渲染的顶点数量。

count 参数不会经过有效性验证,用户有责任确保指定的值仅在 0 到已分配顶点数之间。

调用此函数后,顶点数据不会被置为无效,但调用方必须通过调用 `node->markDirty(QSGNode::DirtyGeometry)` 将几何节点标记为“已修改”,以确保渲染器有机会更新内部缓冲区。

该函数在 Qt 6.10 中引入。

另请参阅 vertexCount()。

void QSGGeometry::setVertexDataPattern(QSGGeometry::DataPattern p)

将顶点的使用模式设置为p 。

默认值为AlwaysUploadPattern 。当设置为默认值以外的任何值时,用户在修改顶点数据后,除了必须调用带有QSGNode::DirtyGeometry 参数的QSGNode::markDirty()外,还必须调用markVertexDataDirty()。

另请参阅 vertexDataPattern()。

int QSGGeometry::sizeOfIndex() const

返回索引类型的字节大小。

当索引类型为 `UnsignedShortType` 时,该值为 `2 `;当索引类型为 `UnsignedIntType` 时,该值为 `4 `。

int QSGGeometry::sizeOfVertex() const

返回一个顶点的字节大小。

该值来自属性。

[static] void QSGGeometry::updateColoredRectGeometry(QSGGeometry *g, const QRectF &rect)

使用rect 中的坐标更新几何体g 。

该函数假设几何体对象包含一个由QSGGeometry::ColoredPoint2D 个顶点组成的三角带。

[static] void QSGGeometry::updateRectGeometry(QSGGeometry *g, const QRectF &rect)

使用rect 中的坐标更新几何体g 。

该函数假设几何体对象包含一个由QSGGeometry::Point2D 个顶点组成的单个三角带

[static] void QSGGeometry::updateTexturedRectGeometry(QSGGeometry *g, const QRectF &rect, const QRectF &textureRect)

使用rect 中的坐标以及textureRect 中的纹理坐标,更新几何体g 。

textureRect 应为归一化坐标。

g 假设其为由四个类型为QSGGeometry::TexturedPoint2D 的顶点组成的三角带。

int QSGGeometry::vertexCount() const

返回可渲染的顶点数量;如果使用了索引,则返回可通过索引访问的顶点数量。

另请参阅 setVertexCount()。

void *QSGGeometry::vertexData()

返回指向该几何体对象原始顶点数据的指针。

另请参阅 vertexDataAsPoint2D() 和vertexDataAsTexturedPoint2D()。

const void *QSGGeometry::vertexData() const

返回指向该几何体对象原始顶点数据的指针。

另请参阅 vertexDataAsPoint2D() 和vertexDataAsTexturedPoint2D()。

QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D()

一个便捷函数,用于将顶点数据作为QSGGeometry::ColoredPoint2D 的可变数组进行访问。

const QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D() const

一个便捷函数,用于将顶点数据作为不可变的QSGGeometry::ColoredPoint2D 数组进行访问。

QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D()

一个便捷函数,用于将顶点数据作为QSGGeometry::Point2D 的可变数组进行访问。

const QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D() const

一个便捷函数,用于将顶点数据作为QSGGeometry::Point2D 的不可变数组进行访问。

QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D()

一个便捷函数,用于将顶点数据作为QSGGeometry::TexturedPoint2D 的可变数组进行访问。

const QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D() const

一个便捷函数,用于将顶点数据作为QSGGeometry::TexturedPoint2D 的不可变数组进行访问。

QSGGeometry::DataPattern QSGGeometry::vertexDataPattern() const

返回该几何体中顶点的使用模式。默认模式为AlwaysUploadPattern 。

另请参阅 setVertexDataPattern()。

© 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.