How Access components inside NodeInstantiator Delegate?

Viewed 273

I have a line class in C++ and I want to use it in qml. I want to draw a line by mouse and have multiple lines. In fact, I want to new my line class, so I use NodeInstantiator.

 ListModel {
    id: entityModel

   }
 NodeInstantiator {
    id: instance

    model: entityModel

    delegate: Entity {
        id: sphereEntity

        components: [
            Line { id:lineMesh } ,

            PhongMaterial { id: material; ambient:"red" },

            Transform { id: transform;  }
        ]
    }
}

My problem is that I can't use Line Component with its id lineMesh outside NodeInstantiator, and also I don't know how to generate lines and add them to its entityModel.

If I don't use NodeInstantiator , when I draw lines by left button of mouse and then use Right Button drawing stops. then in the second time , when I use left button I want New Line Entity .

enter image description here

As picture shows now I can draw line just one time .

2 Answers

I Fixed My problem in CPP , which means that I create a class that plays a wrapper role in my program and It is responsible for collecting points . I new my line class here .

There is a bug in Qt3D NodeInstantiator: It will crash when used with ObjectModel. Thus, there is an explicit ListModel at the moment.

This is probably not a complete answer, since you question in fact has two questions. But I think it will help you in the end.

In order to draw a line, you need two coordinates. I suggest to store the current coordinate when you click the mouse, and when the next click occurs, make a line from the stored coordinate to the current coordinate (Which probably changed). Then you can add an item to your ListModel using the append function:

MouseArea {

    property var lastPoint : undefined

    onClicked: {
         if(lastPoint === undefined) //first click
         {
             lastPoint = {"x": mouse.x, "y": mouse.y}
         }
         else
         {
             entityModel.append({"startX": lastPoint.x,
                                 "startY": lastPoint.y,
                                 "endX": mouse.x,
                                 "endY": mouse.y});
             lastPoint = undefined
         }
    }
}

There are of course variations on this, for example using the moved signal on the MouseArea to insert entities whenever the mouse moved, as long as the left button is pressed. I'll leave that up to you as an exercise.

Related