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. Relative position of Draggable child item when parent resized
Forum Updated to NodeBB v4.3 + New Features

Relative position of Draggable child item when parent resized

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 939 Views 1 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.
  • M Offline
    M Offline
    Mammamia
    wrote on last edited by
    #1

    I have a scenario in which I have a drag-able component(Rectangle) on a main window.

    Initially the application is of size 320x480 and the rectangle is dragged to the bottom right corner of the window.

    When I resize the window or make it fullscreen then I could see that the Rectangle is at same position in the fullscreen window.

    The mentioned functionality can be done by anchoring it to the right but I could not use it as I need to drag the item around.

    0_1505407673793_Rectangle_Rel_Pos.png

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      Hi! I'm pretty sure you have to do this manually, e.g.:

      main.qml

      import QtQuick 2.7
      import QtQuick.Controls 2.0
      import QtQuick.Layouts 1.3
      
      ApplicationWindow {
          visible: true
          width: 640
          height: 480
          color: "black"
      
          DropArea {
              id: dropArea
              anchors.fill: parent
          }
      
          MyRect {
              color: "orange"
              theDropArea: dropArea
          }
      
          MyRect {
              color: "purple"
              theDropArea: dropArea
          }
      }
      

      MyRect.qml

      import QtQuick 2.7
      
      Rectangle {
          id: myrect
      
          property var theDropArea: null
          property real relativeX: 0
          property real relativeY: 0
      
          width: 50
          height: 50
          onXChanged: relativeX = x / theDropArea.width
          onYChanged: relativeY = y / theDropArea.height
      
          Drag.active: mouseArea.drag.active
          Drag.hotSpot.x: width / 2
          Drag.hotSpot.y: height / 2
      
          MouseArea {
              id: mouseArea
              anchors.fill: parent
              drag.target: parent
          }
      
          Connections {
              target: myrect.theDropArea
              onWidthChanged: myrect.x = myrect.theDropArea.width * myrect.relativeX
              onHeightChanged: myrect.y = myrect.theDropArea.height * myrect.relativeY
          }
      }
      
      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mammamia
        wrote on last edited by
        #3

        @Wieland Thanks alot for the snippet. This is almost what I want. I need to give it a try to my application.

        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