Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Dynamically add Entity to 3d scene
QtWS25 Last Chance

Dynamically add Entity to 3d scene

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 1 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HB76
    wrote on 10 Nov 2019, 16:42 last edited by
    #1

    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)"));
        dialog.setFileMode(QFileDialog::ExistingFiles);
        QStringList fileNames;
        if (dialog.exec())
        {
            qDebug() << "Filenames :";
            fileNames = dialog.selectedFiles();
            for (int i=0;i<fileNames.size();i++)
            {
                qDebug() << fileNames.at(i);
            }
        }
    }
    

    bug.png

    1 Reply Last reply
    0
    • H Offline
      H Offline
      HB76
      wrote on 10 Nov 2019, 22:46 last edited by
      #2

      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
      
      Entity
      {
          property string name
          property string url
          property color materialColor
          property bool selected
      
          id : name
      
          ObjectPicker
          {
              id: picker
              hoverEnabled: true
              onClicked:
              {
                  if (selected) selected = false
                  else selected = true
              }
          }
      
          PhongAlphaMaterial
          {
              id: material
              diffuse: materialColor
              alpha: 1
              shininess: 0
          }
      
          Mesh
          {
              id: mesh
              source: url
          }
      
          Transform
          {
              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 ?

      1 Reply Last reply
      0

      1/2

      10 Nov 2019, 16:42

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved