3

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 .

piet.t
  • 11,718
  • 21
  • 43
  • 52
Parisa.H.R
  • 3,303
  • 3
  • 19
  • 38
  • Please edit the post to explain why you need to refer to the `lineMesh` – Amfasis Jun 29 '21 at 07:01
  • I want to use the functions of this component. – Parisa.H.R Jun 29 '21 at 07:18
  • maybe you can reach a function in the delegate using [objectAt](https://doc.qt.io/qt-5/qml-qt3d-core-nodeinstantiator.html#objectAt-method). But then I doni't know if it possible to get to the `lineMesh` – Amfasis Jun 29 '21 at 07:20
  • No, it doesn't work , I test it. – Parisa.H.R Jun 29 '21 at 07:27
  • How about a different idea: https://doc.qt.io/qt-5/qml-qtqml-models-objectmodel.html ? You will have to do `createComponent`, so a bit more coding – Amfasis Jun 29 '21 at 07:32
  • Do you mean that I don't use NodeInstantiator ? I cant understand and little confused , NodeInstantiator shouldn't create objects ? – Parisa.H.R Jun 29 '21 at 08:05
  • it might be a stupid idea of me, but I thought maybe you can switch ListModel to ObjectModel and instantiate the Line, PhongMaterial and Transform when you add it to the `entityModel`, in which case you can call the function on the line – Amfasis Jun 29 '21 at 08:29
  • 1
    look at this [github example](https://github.com/MASKOR/mapit/blob/e4f67c4da9e28fbf2c26604398e7705803956e7d/tools/gui/qml/ApplicationState.qml) , and [this](https://github.com/MASKOR/mapit/blob/e4f67c4da9e28fbf2c26604398e7705803956e7d/tools/visualization/qml/network/MapitMultiviewNetworkState.qml) . They say : There is a bug in Qt3D NodeInstantiator: It will crash when used with ObjectModel. Thus, there is an explicit ListModel at the moment – Parisa.H.R Jun 29 '21 at 10:03

2 Answers2

1

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.

Parisa.H.R
  • 3,303
  • 3
  • 19
  • 38
-1

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.

Amfasis
  • 3,932
  • 2
  • 18
  • 27
  • I use qt3d and should use `MouseHandler` instead of `MouseArea` and also I want to use functions that are inside my line component class. – Parisa.H.R Jun 29 '21 at 07:06
  • Ok, then this answer only serves to explain the `append` function ;-) – Amfasis Jun 29 '21 at 07:21