Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to deal with anchoring while making a QML item Draggable?

How to deal with anchoring while making a QML item Draggable?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
5 Posts 2 Posters 1.1k 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.
  • Roby BrundleR Offline
    Roby BrundleR Offline
    Roby Brundle
    wrote on last edited by Roby Brundle
    #1

    I have QML which host an image. I need to allow the user to pan the image. Basically be able to drag it around.

    I figured that Drag QML type gives me this capability. It works as well using something like below:

    MyQmlComponent {
        id: myqmlitem
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter
    
        Drag.dragType: Drag.Automatic
    
        MouseArea {
            anchors.fill: parent
            drag.target: myqmlitem
    
            drag {
                axis: Drag.XandYAxis
    
                onActiveChanged: {
                    console.log("Active changed..")
                     
                    anchors.top = undefined
                    anchors.horizontalCenter = undefined
                    // I want to unset the anchors here and reset it back at a later stage. But how?
                }
            }
        }
    }
    

    I figured the anchoring properties are coming in the way of my drag/pan feature. How should I deal with this? Are there any ways to set and unset the anchors? Doing the below doesn't seem to work.

    anchors.top = undefined
    anchors.horizontalCenter = undefined
    

    Basically, I want to unset the anchors when drag is active. And unset it at a later stage. Please suggest if there are other ways to deal with this as well..

    raven-worxR 1 Reply Last reply
    0
    • Roby BrundleR Roby Brundle

      I have QML which host an image. I need to allow the user to pan the image. Basically be able to drag it around.

      I figured that Drag QML type gives me this capability. It works as well using something like below:

      MyQmlComponent {
          id: myqmlitem
          anchors.top: parent.top
          anchors.horizontalCenter: parent.horizontalCenter
      
          Drag.dragType: Drag.Automatic
      
          MouseArea {
              anchors.fill: parent
              drag.target: myqmlitem
      
              drag {
                  axis: Drag.XandYAxis
      
                  onActiveChanged: {
                      console.log("Active changed..")
                       
                      anchors.top = undefined
                      anchors.horizontalCenter = undefined
                      // I want to unset the anchors here and reset it back at a later stage. But how?
                  }
              }
          }
      }
      

      I figured the anchoring properties are coming in the way of my drag/pan feature. How should I deal with this? Are there any ways to set and unset the anchors? Doing the below doesn't seem to work.

      anchors.top = undefined
      anchors.horizontalCenter = undefined
      

      Basically, I want to unset the anchors when drag is active. And unset it at a later stage. Please suggest if there are other ways to deal with this as well..

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Roby-Brundle
      according to the docs assigning undefined to anchors is the correct way to reset them.

      What is the exact behavior in your case?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      Roby BrundleR 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @Roby-Brundle
        according to the docs assigning undefined to anchors is the correct way to reset them.

        What is the exact behavior in your case?

        Roby BrundleR Offline
        Roby BrundleR Offline
        Roby Brundle
        wrote on last edited by Roby Brundle
        #3

        @raven-worx setting it to undefined doesn't help. I have the undefined set already in the code. it doesn't seem to work at all.
        Seems like the anchors i am trying to set and unset do not take affect on myqmlitem ?

        raven-worxR 1 Reply Last reply
        0
        • Roby BrundleR Roby Brundle

          @raven-worx setting it to undefined doesn't help. I have the undefined set already in the code. it doesn't seem to work at all.
          Seems like the anchors i am trying to set and unset do not take affect on myqmlitem ?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @Roby-Brundle
          and i ask again (one last time):

          What is the exact behavior in your case?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          Roby BrundleR 1 Reply Last reply
          0
          • raven-worxR raven-worx

            @Roby-Brundle
            and i ask again (one last time):

            What is the exact behavior in your case?

            Roby BrundleR Offline
            Roby BrundleR Offline
            Roby Brundle
            wrote on last edited by
            #5

            @raven-worx The exact behaviour was that the default anchors which are the following

            anchors.top: parent.top
            anchors.horizontalCenter: parent.horizontalCenter
            

            were not getting unset by doing setting it undefined. That means that myqmlcomponent remained anchored to top and horizontalCenter. if they were really getting unset then I would be able to drag myqmlcomponent to different X and Y coordinates.

            But the good news is that I figured this out. I needed to refer to myqmlcomponent when setting it anchors to undefined. Like below:

            onActiveChanged: {
                myqmlcomponent.anchors.top = undefined
                myqmlcomponent.anchors.horizontalCenter = undefined
            }
            

            Now it works fine to unset or reset the anchors well

            1 Reply Last reply
            1

            • Login

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