- Qt 6.11
- Qt Quick
- C++ Classes
- QSGGeometry
- Qt 6.11.0 Reference Documentation

# QSGGeometry Class

The QSGGeometry class provides low-level storage for graphics primitives in the Qt Quick Scene Graph. More...

Header: | `#include <QSGGeometry>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Quick)` `target_link_libraries(mytarget PRIVATE Qt6::Quick)`
qmake: | `QT += quick`

- List of all members, including inherited members

## Public Types

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 }

## Public Functions

| 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

## Static Public Members

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)

## Detailed Description

The QSGGeometry class stores the geometry of the primitives rendered with the scene graph. It contains vertex data and optionally index data. The mode used to draw the geometry, also called primitive topology, is specified with setDrawingMode().

Vertices can be as simple as points defined by x and y values or can be more complex where each vertex contains a normal, texture coordinates and a 3D position. The QSGGeometry::AttributeSet is used to describe how the vertex data is built up. The attribute set can only be specified on construction. The QSGGeometry class provides a few convenience attributes and attribute sets by default. The defaultAttributes_Point2D() function returns an attribute set to be used in normal solid color rectangles, while the defaultAttributes_TexturedPoint2D function returns attributes to be used for textured 2D geometry. The vertex data is internally stored as a `void *` and is accessible with the vertexData() function. Convenience accessors for the common attribute sets are available with vertexDataAsPoint2D() and vertexDataAsTexturedPoint2D(). Vertex data is allocated by passing a vertex count to the constructor or by calling allocate() later.

The number of vertices and indices can be changed after construction by using the allocate() method to resize the data buffer. However, allocate() requires updating all vertex and index data each time called. Since Qt 6.10, setVertexCount() and setIndexCount() allow adjusting the number of vertices or indices without reallocating the data buffer and only require updating new vertices or indices. In either case, the caller must mark the geometry node as dirty, by calling `node->markDirty(QSGNode::DirtyGeometry)`, to ensure that the renderer has a chance to update internal buffers.

The QSGGeometry can optionally contain indices of either unsigned 32-bit, unsigned 16-bit, or unsigned 8-bit integers. The index type must be specified during construction and cannot be changed.

Below is a snippet illustrating how a geometry composed of position and color vertices can be built.

     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);

The QSGGeometry is a software buffer and client-side in terms of accelerated rendering, as the buffers used in 2D graphics typically consist of many small buffers that change every frame and do not benefit from being uploaded to graphics memory. However, the QSGGeometry supports hinting to the renderer that a buffer should be uploaded using the setVertexDataPattern() and setIndexDataPattern() functions. Whether this hint is respected or not is implementation specific.

Note: All classes with QSG prefix should be used solely on the scene graph's rendering thread. See Scene Graph and Rendering for more information.

See also QSGGeometryNode and Scene Graph - Custom Geometry.

## Member Type Documentation

### enum QSGGeometry::AttributeType

This enum identifies several attribute types.

Constant | Value | Description
--- | --- | ---
`QSGGeometry::UnknownAttribute` | `0` | Don't care
`QSGGeometry::PositionAttribute` | `1` | Position
`QSGGeometry::ColorAttribute` | `2` | Color
`QSGGeometry::TexCoordAttribute` | `3` | Texture coordinate
`QSGGeometry::TexCoord1Attribute` | `4` | Texture coordinate 1
`QSGGeometry::TexCoord2Attribute` | `5` | Texture coordinate 2

### enum QSGGeometry::DataPattern

The DataPattern enum is used to specify the use pattern for the vertex and index data in a geometry object.

Constant | Value | Description
--- | --- | ---
`QSGGeometry::AlwaysUploadPattern` | `0` | The data is always uploaded. This means that the user does not need to explicitly mark index and vertex data as dirty after changing it. This is the default.
`QSGGeometry::DynamicPattern` | `2` | The data is modified repeatedly and drawn many times. This is a hint that may provide better performance. When set the user must make sure to mark the data as dirty after changing it.
`QSGGeometry::StaticPattern` | `3` | The data is modified once and drawn many times. This is a hint that may provide better performance. When set the user must make sure to mark the data as dirty after changing it.
`QSGGeometry::StreamPattern` | `1` | The data is modified for almost every time it is drawn. This is a hint that may provide better performance. When set, the user must make sure to mark the data as dirty after changing it.

### enum QSGGeometry::DrawingMode

Specifies the drawing mode, also called primitive topology.

Note: Starting with Qt 6 the scene graph only exposes topologies that are supported across all the supported 3D graphics APIs. As a result, the values `DrawLineLoop` and `DrawTriangleFan` are no longer supported at run time in Qt 6, even though the enum values themselves are still present.

Constant | Value
--- | ---
`QSGGeometry::DrawPoints` | `0x0000`
`QSGGeometry::DrawLines` | `0x0001`
`QSGGeometry::DrawLineStrip` | `0x0003`
`QSGGeometry::DrawTriangles` | `0x0004`
`QSGGeometry::DrawTriangleStrip` | `0x0005`

### enum QSGGeometry::Type

Specifies the component type in the vertex data.

Constant | Value | Description
--- | --- | ---
`QSGGeometry::ByteType` | `0x1400` |
`QSGGeometry::UnsignedByteType` | `0x1401` |
`QSGGeometry::ShortType` | `0x1402` |
`QSGGeometry::UnsignedShortType` | `0x1403` |
`QSGGeometry::IntType` | `0x1404` |
`QSGGeometry::UnsignedIntType` | `0x1405` |
`QSGGeometry::FloatType` | `0x1406` |
`QSGGeometry::Bytes2Type` | `0x1407` | Added in Qt 5.14.
`QSGGeometry::Bytes3Type` | `0x1408` | Added in Qt 5.14.
`QSGGeometry::Bytes4Type` | `0x1409` | Added in Qt 5.14.
`QSGGeometry::DoubleType` | `0x140A` | Added in Qt 5.14.

## Member Function Documentation

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

Constructs a geometry object based on attributes.

The object allocate space for vertexCount vertices based on the accumulated size in attributes and for indexCount.

The indexType can be UnsignedShortType or `UnsignedIntType`. Support for the latter depends on the graphics API implementation used at run time, and may not always be available.

Geometry objects are constructed by default with DrawTriangleStrip as the drawing mode.

Note: attributes and the Attribute objects referenced by it must stay valid for the entire lifetime of the QSGGeometry. QSGGeometry stores a reference to attributes and does not delete the Attribute objects.

### `[virtual noexcept]` QSGGeometry::~QSGGeometry()

Destroys the geometry object and the vertex and index data it has allocated.

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

Resizes the vertex and index data of this geometry object to fit vertexCount vertices and indexCount indices and sets the number of vertices and indices accordingly.

Use setVertexCount() or setIndexCount() to change the number of vertices or indices without calling allocate() again.

Vertex and index data will be invalidated after this call and the caller must mark the associated geometry node as dirty, by calling `node->markDirty(QSGNode::DirtyGeometry)`, to ensure that the renderer has a chance to update internal buffers.

See also setVertexCount() and setIndexCount().

### int QSGGeometry::attributeCount() const

Returns the number of attributes in the attrbute set used by this geometry.

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

Returns an array with the attributes of this geometry. The size of the array is given with attributeCount().

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

Convenience function which returns attributes to be used for per vertex colored 2D drawing.

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

Convenience function which returns attributes to be used for 2D solid color drawing.

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

Convenience function which returns attributes to be used for textured 2D drawing.

### unsigned int QSGGeometry::drawingMode() const

Returns the drawing mode of this geometry.

The default value is DrawTriangleStrip.

See also setDrawingMode().

### int QSGGeometry::indexCount() const

Returns the number of indices that are processed when the geometry object is rendered.

See also setIndexCount().

### void *QSGGeometry::indexData()

Returns a pointer to the raw index data of this geometry object.

See also indexDataAsUShort() and indexDataAsUInt().

### const void *QSGGeometry::indexData() const

Returns a pointer to the raw index data of this geometry object.

See also indexDataAsUShort() and indexDataAsUInt().

### uint *QSGGeometry::indexDataAsUInt()

Convenience function to access the index data as a mutable array of 32-bit unsigned integers.

### const uint *QSGGeometry::indexDataAsUInt() const

Convenience function to access the index data as an immutable array of 32-bit unsigned integers.

### quint16 *QSGGeometry::indexDataAsUShort()

Convenience function to access the index data as a mutable array of 16-bit unsigned integers.

### const quint16 *QSGGeometry::indexDataAsUShort() const

Convenience function to access the index data as an immutable array of 16-bit unsigned integers.

### QSGGeometry::DataPattern QSGGeometry::indexDataPattern() const

Returns the usage pattern for indices in this geometry. The default pattern is AlwaysUploadPattern.

See also setIndexDataPattern().

### int QSGGeometry::indexType() const

Returns the primitive type used for indices in this geometry object.

### float QSGGeometry::lineWidth() const

Gets the current line or point width or to be used for this geometry. This property only applies to line width when the drawingMode is DrawLines or DrawLineStrip. When supported, it also applies to point size when the drawingMode is DrawPoints.

The default value is `1.0`

Note: Support for point and line drawing may be limited at run time, depending on the platform and graphics API. For example, some APIs do not support point sprites and so setting a size other than 1 is not possible.

Note: The width of `1.0` is always supported.

See also setLineWidth() and drawingMode().

### void QSGGeometry::markIndexDataDirty()

Mark that the vertices in this geometry has changed and must be uploaded again.

This function only has an effect when the usage pattern for vertices is StaticData and the renderer that renders this geometry uploads the geometry into Vertex Buffer Objects (VBOs).

### void QSGGeometry::markVertexDataDirty()

Mark that the vertices in this geometry has changed and must be uploaded again.

This function only has an effect when the usage pattern for vertices is StaticData and the renderer that renders this geometry uploads the geometry into Vertex Buffer Objects (VBOs).

### void QSGGeometry::setDrawingMode(unsigned int mode)

Sets the mode to be used for drawing this geometry.

The default value is QSGGeometry::DrawTriangleStrip.

See also drawingMode() and DrawingMode.

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

Sets the number of indices to be processed each time the geometry object is rendered.

The count is not validated and it is the users responsibility to ensure that only values between zero and the number of allocated indices are specified.

Vertex and index data is not invalidated after this call, but the caller must mark the geometry node as dirty, by calling `node->markDirty(QSGNode::DirtyGeometry)`, to ensure that the renderer has a chance to update internal buffers.

This function was introduced in Qt 6.10.

See also indexCount().

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

Sets the usage pattern for indices to p.

The default is AlwaysUploadPattern. When set to anything other than the default, the user must call markIndexDataDirty() after changing the index data, in addition to calling QSGNode::markDirty() with QSGNode::DirtyGeometry.

See also indexDataPattern().

### void QSGGeometry::setLineWidth(float width)

Sets the line or point width to be used for this geometry to width. This property only applies to line width when the drawingMode is DrawLines or DrawLineStrip. When supported, it also applies to point size when the drawingMode is DrawPoints.

Note: Support for point and line drawing may be limited at run time, depending on the platform and graphics API. For example, some APIs do not support point sprites and so setting a size other than 1 is not possible.

Note: The width of `1.0` is always supported.

See also lineWidth() and drawingMode().

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

Sets the number of vertices to be rendered.

The count is not validated and it is the users responsibility to ensure that only values between zero and the number of allocated vertices are specified.

Vertex data is not invalidated after this call, but the caller must mark the geometry node as dirty, by calling `node->markDirty(QSGNode::DirtyGeometry)`, to ensure that the renderer has a chance to update internal buffers.

This function was introduced in Qt 6.10.

See also vertexCount().

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

Sets the usage pattern for vertices to p.

The default is AlwaysUploadPattern. When set to anything other than the default, the user must call markVertexDataDirty() after changing the vertex data, in addition to calling QSGNode::markDirty() with QSGNode::DirtyGeometry.

See also vertexDataPattern().

### int QSGGeometry::sizeOfIndex() const

Returns the byte size of the index type.

This value is either `2` when the index type is UnsignedShortType, or `4` when the index type is UnsignedIntType.

### int QSGGeometry::sizeOfVertex() const

Returns the size in bytes of one vertex.

This value comes from the attributes.

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

Updates the geometry g with the coordinates in rect.

The function assumes the geometry object contains a single triangle strip of QSGGeometry::ColoredPoint2D vertices

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

Updates the geometry g with the coordinates in rect.

The function assumes the geometry object contains a single triangle strip of QSGGeometry::Point2D vertices

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

Updates the geometry g with the coordinates in rect and texture coordinates from textureRect.

textureRect should be in normalized coordinates.

g is assumed to be a triangle strip of four vertices of type QSGGeometry::TexturedPoint2D.

### int QSGGeometry::vertexCount() const

Returns the number of vertices that can be rendered, or if indices are used, it returns the number of vertices that can be accessed through indices.

See also setVertexCount().

### void *QSGGeometry::vertexData()

Returns a pointer to the raw vertex data of this geometry object.

See also vertexDataAsPoint2D() and vertexDataAsTexturedPoint2D().

### const void *QSGGeometry::vertexData() const

Returns a pointer to the raw vertex data of this geometry object.

See also vertexDataAsPoint2D() and vertexDataAsTexturedPoint2D().

### QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D()

Convenience function to access the vertex data as a mutable array of QSGGeometry::ColoredPoint2D.

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

Convenience function to access the vertex data as an immutable array of QSGGeometry::ColoredPoint2D.

### QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D()

Convenience function to access the vertex data as a mutable array of QSGGeometry::Point2D.

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

Convenience function to access the vertex data as an immutable array of QSGGeometry::Point2D.

### QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D()

Convenience function to access the vertex data as a mutable array of QSGGeometry::TexturedPoint2D.

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

Convenience function to access the vertex data as an immutable array of QSGGeometry::TexturedPoint2D.

### QSGGeometry::DataPattern QSGGeometry::vertexDataPattern() const

Returns the usage pattern for vertices in this geometry. The default pattern is AlwaysUploadPattern.

See also setVertexDataPattern().