Important: Please read the Qt Code of Conduct -

How to get vertices, normals and indeces from Qt3DRender::QAttribute buffer of Qt3DRender::QGeometry

  • Hi,

    I thought will be simple task to get vertices, normals and indeces from any geometry in Qt3D. It was clear for me that I can get the components from particular Entity and from there to query for QGeometryRender,to get QAttribute with name vertexPosition , get Qt3DRender::QBuffer -> get it like QByteArray and then simply typecast it and parse it.
    The fact is I spent 4 hours and the buffer data comes empty/ size 0/. I tried with Qt3DExtras::QPlaneMesh - just to be simple clear - to get 1 or 2 triangles - but nothing comes .... on the other side the geometry is send to GPU and is drawn.

    Here is a snippet code

    const Qt3DRender::QGeometry *geometry = planeMesh->geometry();
    	for (Qt3DRender::QAttribute* attribute : geometry->attributes())
    	    /// just for information....
    		qDebug() <<attribute ->name();
    		qDebug() << attribute->vertexBaseType();
    		qDebug() << attribute->vertexSize();
    		qDebug() << attribute->buffer()->usage();
    		qDebug() << attribute->buffer()->accessType();
    		qDebug() << attribute->buffer()->data().length();/// here return 0
    		if (attribute->name() == Qt3DRender::QAttribute::defaultPositionAttributeName())
    		    Qt3DRender::QBuffer *buffer = attribute->buffer();
    		    QByteArray vertexArray = buffer->data();
    	/// --- this way 
    		     float *reVertexArray = reinterpret_cast<float*>(;		
            /// --- or this way 		
    	             int byteOffsetPos = attribute->byteOffset();
    		     int byteStridePos = attribute->byteStride();
    		    if (buffer->type() == Qt3DRender::QBuffer::VertexBuffer)
    				quint32 trianglesCount = attribute->count()/ 3;
    				for (int j = 0; j < trianglesCount; ++j) 
    					int idxPos = byteOffsetPos + j * 3 * byteStridePos;	

    I search on internet - mainly StackOverflow and found this

    and this

    I tried both of them... both of them not working.
    Can somebody help and post some solution.
    That will be really helpful, having in mind that Qt help or any information about Qt3D in c++ is close to nothing.


  • @julianm

    You will have to set setSyncData(true); on the attribute-buffers and also synch the index buffers to get the right vertices.

Log in to reply