Solved connect methods for dynamically created objects
-
Hi
I have created an object from "MarkerItem.qml" and I want to connect a signal to "delete" in the popup menu.
This is my "MarkerItem.qml"
MapQuickItem{ id: _marker anchorPoint.x: _markerImage.width/2 anchorPoint.y: _markerImage.height/2 sourceItem: Image { id: _markerImage source: "qrc:/marker.png" opacity: 0.9 Drag.active:_markerMouseArea.drag.active } Menu{ id: _deleteMenu MenuItem{ text: qsTr('Delete') shortcut: StandardKey.Delete /*onTriggered: { console.log("delete button pressed") }*/ } } MouseArea{ anchors.fill: _marker id: _markerMouseArea drag.target: _marker preventStealing: true acceptedButtons: Qt.RightButton onClicked: { _deleteMenu.popup() } } }
I have created the objects using the following code:
function myMethod() { console.log("Delete Button was clicked!") } var component = Qt.createComponent("MarkerItem.qml"); if (component.status === Component.Ready) { var markerItem = component.createObject(_map); markerItem.coordinate = _map.toCoordinate(Qt.point(_flightMapMouseArea.mouseX,_flightMapMouseArea.mouseY)) //want to connect the mymethod here //markerItem.on }
-
Hi! The syntax for this is: <object>.<signal>.connect( <function> ), e.g:
backend.toolChanged.connect( recalibrate )
Note that there are no parentheses after "<function>".
-
@saitej One simple way would be to call
myMethod
directly fromonTriggered
handler.onTriggered: _marker.parent.myMethod()
Since from the component creation code I see
_marker
is a child of_map
. And assuming_map
contains the methodmyMethod
-
@p3c0
Thanks! This is working. How to get the object (marker) index on which the method is triggered to delete that object. -
How to get the object (marker) index on which the method is triggered to delete that object.
Well you can directly get the item itself. You just need to modify the caller function a little bit. Eg.
function myMethod(obj) { console.log(obj) }
and call it as follows by sending
this
onTriggered: _marker.parent.myMethod(this)
You can then check specific properties of
MenuItem
inside themyMethod
function. eg.obj.text
-
@p3c0
Thanks!I had done it using myMethod(_marker). It's working fine but I get this warning "QML Binding: Property 'raised' does not exist on Item." every time I click on delete.
Also, Drag function is not working on the MarkerItem (MapQuickItem). I have put
drag.target: _marker
in the mouse area of the marker. Do I need to activate any other drag function?
-
I had done it using myMethod(_marker)
Ok. But passing
this
would be better.It's working fine but I get this warning "QML Binding: Property 'raised' does not exist on Item." every time I click on delete.
Looks like a known problem. See QTBUG-50100. It seems Menu's are affected due to some reason.
Also, Drag function is not working on the MarkerItem (MapQuickItem). I have put
drag.target: _marker
in the mouse area of the marker. Do I need to activate any other drag function?Did you try dragging by clicking Right Mouse Button because that is what you are only accepting. viz.
acceptedButtons: Qt.RightButton
Remove it or add support for both. -
Hi ...
One more small issue. I have 2 functions. " getPolygonIndex(obj)" is called on press and hold & "moveMarkerItem(obj, mx,my)" on released. These are used to drag the marker and also change the coordinate of the mappolygon.
But it seems to change randomly. Any possible explanation/ suggestions please.function getPolygonIndex(obj){ var path = _mapPolygon.path; for(var i=0; i < path.length; i++) { if((path[i].latitude === obj.coordinate.latitude) && (path[i].longitude === obj.coordinate.longitude) ) currentIndexPolygon = i } } // this function is called on mouse release // args onj = _marker, mx, my = markermousearea.mousex ,y function moveMarkerItem(obj, mx,my) { console.log(currentIndexPolygon) var path = _mapPolygon.path; var coord = _map.toCoordinate(Qt.point(mx,my)) console.log("before change",coord.latitude,path[currentIndexPolygon].latitude); path[currentIndexPolygon].latitude = coord.latitude path[currentIndexPolygon].longitude = coord.longitude console.log("after change",coord.latitude,path[currentIndexPolygon].latitude); _mapPolygon.path = path; }
-
@saitej Well the code snippet is not enough to find the exact cause.
May be you should also try to look at mapFromItem or mapToItem. These are basically used to adjust the coordinates of child w.r.t parent.