how to load an STL file using QMesh

  • Hi all,

    I'm really new to Qt, using it only just a couple of weeks.

    I'm trying to find a way to load STL (stereolithography) files and display them. I've checked the documentation but there is no example of how to implement it.

    Is there any tutorial or past example that I could study for doing this?

    Thank you in advance for any help.

  • @Dimis I'd say you can have a look at the variety of Qt 3D examples to get used to the framework, and then you may also take a look at some previous posts: here and here.

  • Moderators

    I personally haven't used it yet, but by looking at the docs you should use the source property

    QMesh mesh = ...;
    mesh.setSource( QUrl::fromLocalFile("path/to/local/file.stl") );

  • Thank you for your responses. I'll give a try.

  • I've sort it out. Just for anyone interested in this, my code for loading STL files is below.

    QGuiApplication a(argc, argv);
    QUrl data = QUrl::fromLocalFile("C:/Qt/Examples/objloader2/cadtrainingblock24asc.stl");

    Qt3DExtras::Qt3DWindow view;
    Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;
    Qt3DCore::QEntity *flyingwedge = new Qt3DCore::QEntity(rootEntity);
    Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial();
    material->setDiffuse(QColor(254, 254, 254));
    Qt3DRender::QMesh *flyingwedgeMesh = new Qt3DRender::QMesh;
    Qt3DRender::QCamera *camera =;
    camera->lens()->setPerspectiveProjection(40.0f, 16.0f/9.0f, 0.1f, 1000.0f);
    camera->setPosition(QVector3D(0, 0, 40.0f));
    camera->setViewCenter(QVector3D(0, 0, 0));
    Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(rootEntity);
    Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);
    Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity);
    lightTransform->setTranslation(QVector3D(60, 0, 40.0f));
    Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity);
    return a.exec();

  • Do you know how I can get the QVector3D from the "Entity"? I want to set the camera position (camera-> setPosition) with a dynamic QVector3D using the geometry of the scene entity

Log in to reply