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. connect methods for dynamically created objects
Forum Updated to NodeBB v4.3 + New Features

connect methods for dynamically created objects

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 3 Posters 2.6k Views 2 Watching
  • 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.
  • saitejS Offline
    saitejS Offline
    saitej
    wrote on last edited by
    #1

    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
                    }
    
    p3c0P 1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! The syntax for this is: <object>.<signal>.connect( <function> ), e.g:

      backend.toolChanged.connect( recalibrate )
      

      Note that there are no parentheses after "<function>".

      1 Reply Last reply
      2
      • saitejS saitej

        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
                        }
        
        p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        @saitej One simple way would be to call myMethod directly from onTriggered handler.

        onTriggered: _marker.parent.myMethod()
        

        Since from the component creation code I see _marker is a child of _map. And assuming _map contains the method myMethod

        157

        saitejS 1 Reply Last reply
        1
        • p3c0P p3c0

          @saitej One simple way would be to call myMethod directly from onTriggered handler.

          onTriggered: _marker.parent.myMethod()
          

          Since from the component creation code I see _marker is a child of _map. And assuming _map contains the method myMethod

          saitejS Offline
          saitejS Offline
          saitej
          wrote on last edited by
          #4

          @p3c0
          Thanks! This is working. How to get the object (marker) index on which the method is triggered to delete that object.

          p3c0P 1 Reply Last reply
          0
          • saitejS saitej

            @p3c0
            Thanks! This is working. How to get the object (marker) index on which the method is triggered to delete that object.

            p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            @saitej

            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 the myMethod function. eg. obj.text

            157

            saitejS 1 Reply Last reply
            0
            • p3c0P p3c0

              @saitej

              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 the myMethod function. eg. obj.text

              saitejS Offline
              saitejS Offline
              saitej
              wrote on last edited by
              #6

              @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?

              p3c0P 1 Reply Last reply
              0
              • saitejS saitej

                @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?

                p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                @saitej

                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.

                157

                saitejS 1 Reply Last reply
                0
                • p3c0P p3c0

                  @saitej

                  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.

                  saitejS Offline
                  saitejS Offline
                  saitej
                  wrote on last edited by
                  #8

                  @p3c0

                  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.

                  Before Dragging

                  After Dragging

                   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;
                      }
                  
                  p3c0P 1 Reply Last reply
                  0
                  • saitejS saitej

                    @p3c0

                    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.

                    Before Dragging

                    After Dragging

                     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;
                        }
                    
                    p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #9

                    @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.

                    157

                    1 Reply Last reply
                    0

                    • Login

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