How to render a triangle using Qt3D

  • Hi,

    how can I render a triangle using Qt3D (C++) without loading a obj file?

  • Moderators

    @cjsb Hi, welcome to the Qt forum! You can create your own Qt3D::QGeometry-derived class. There is an example for this, although I must admit, the example is quite complicated. See Qt3D: Tessellation Modes QML Example, especially tessellatedquadmesh.cpp Example File.

  • Thanks Wieland,

    I will try to create the QGeometry. With the new QGeometry done, how can I tell the application to render it? I am not using the QML based code =x

  • Moderators

    Once you got that first thing complete, you need to build a class that inherits from Qt3DRender::QGeometryRenderer. Here is some code I wrote a while ago that demonstrates that:


    #ifndef FEMMESH_H
    #define FEMMESH_H
    #include <Qt3DRender/qgeometryrenderer.h>
    class MeshData;
    class FemMesh : public Qt3DRender::QGeometryRenderer
        explicit FemMesh(Qt3DCore::QNode *parent = 0);
    public slots:
        void setFromMeshData(MeshData *meshData);
        void clear();
        void init();
    #endif // FEMMESH_H


    #include "femmesh.h"
    #include "meshdata.h"
    #include "femmeshgeometry.h"
    #include "meshdataloader.h"
    FemMesh::FemMesh(QNode *parent)
        : Qt3DRender::QGeometryRenderer(parent)
    void FemMesh::setFromMeshData(MeshData *meshData)
        FemMeshGeometry *geom = new FemMeshGeometry(this);
        // setPrimitiveCount has a misleading name.
        // It takes this: (number of triangles) * (number of vertices per triangle)
        setPrimitiveCount(meshData->triangleCount() * 3);
    void FemMesh::clear()
        if (geometry())
    void FemMesh::init()
        MeshDataLoader mdl;
        MeshData * meshData = mdl.load(QUrl(QStringLiteral("qrc:/assets/")));
      //  meshData->dbg(); // print mesh data to debug output
        delete meshData;

    With the two classes at hand you can write a complete Qt3D application in C++, like in the Qt 3D: Simple C++ Example.

  • Great, I will try to create one.

    Can I have access to the meshdata, femmeshgeometry and meshdataloader? Because in the end, I want to render a mesh and those code might help a lot.

  • Moderators

    @cjsb I've sent you a chat message with a download link. Also, there is a thread related to that code:

  • Hi @cjsb! I've created two small examples.
    The first example is pure C++. It is exactly you are looking for -
    The second one is a Qml-based app -

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.