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. Position change when parent resizes
Forum Updated to NodeBB v4.3 + New Features

Position change when parent resizes

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 119 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.
  • JobRecrdJ Offline
    JobRecrdJ Offline
    JobRecrd
    wrote on last edited by
    #1

    How do I properly change the x, y of an object so that it changes its position when the parent is resized? There is, I will introduce that if I drag the rectangle to the middle, then when the window is resized, it should remain in the middle. (middle for example only, rectangle can be moved freely)

    import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick.Controls 2.3
    
    Window {
        visible: true
        width: 640
        height: 480
    
    onWidthChanged: {
                block.x -= block.previousWidth - width
                block.previousWidth = width
            }
    
    onHeightChanged: {
                block.y -= block.previousHeight - height
                block.previousHeight = height
            }
    
        Rectangle {
            id: block
            color: "red"
            width: 50
            height:50
            x: 100
            y: 50
    
           property int previousWidth: 0
           property int previousHeight:0
    
           Component.onCompleted: { 
              previousWidth = parent.width
              previousHeight  = parent.height
    }
    
            MouseArea {
                anchors.fill: parent
                drag.target: block
            }
        }
    }
    
    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