Important: Please read the Qt Code of Conduct -

Dynamically add Entity to 3d scene

  • Hi everyone,

    I am trying to code an stl viewer. So I've defined a Scene3d with a camera, lights... I also put a button over the scene on which I can click to open a new file with a QFileDialog (in my c++ code). Now I would like to create an item and to render it into the scene every time I click on my button, but I have no idea of how to add it dynamically on qml. On top of that, I also have a bug with my QFileDialog object which show icons in black (see picture bellow). Do you have any idea of what can cause this bug and how can I do to load my entity dynamically ?

    Thanks for your help !

    Code for QFileDialog :

    void mainWindow::importModel()
        QFileDialog dialog(this);
        dialog.setNameFilter(tr("3D files (*.stl *.obj *.ply)"));
        QStringList fileNames;
        if (dialog.exec())
            qDebug() << "Filenames :";
            fileNames = dialog.selectedFiles();
            for (int i=0;i<fileNames.size();i++)
                qDebug() <<;


  • I have found a way to create an instance of an Object class using this code :

    function addObject(name,source,color)
            var component = Qt.createComponent("Object.qml");
            var object = component.createObject(scene, { name: name, url: source, materialColor: color, selected: true });

    The Object.qml class is as follow :

    import QtQuick 2.0
    import Qt3D.Core 2.0
    import Qt3D.Render 2.0
    import Qt3D.Input 2.0
    import Qt3D.Extras 2.0
        property string name
        property string url
        property color materialColor
        property bool selected
        id : name
            id: picker
            hoverEnabled: true
                if (selected) selected = false
                else selected = true
            id: material
            diffuse: materialColor
            alpha: 1
            shininess: 0
            id: mesh
            source: url
            id: transform
            scale3D: Qt.vector3d(1, 1, 1)
            translation: Qt.vector3d(0, 0, 0)
            rotation: fromAxisAndAngle(Qt.vector3d(1, 0, 0), 0)
        components: [ mesh, material, transform, picker ]

    But now I have another bug with the ObjectPicker, the object is selected whenever I click on the mesh or not (I tried different picking method and the result is still the same).

    Does anyone has already met this problem or knows how to solve it ?

Log in to reply