拖动项目的中心

时间:2018-11-19 15:17:25

标签: qt drag-and-drop qml qtquick2 drag

我的Item有点大。我想在拖动时调整大小,并确保始终拖动 center 。以下是一个试用版(为简单起见,这里我没有将Item的中心设置为鼠标位置,也没有更改大小。),但是位置x将是更改后立即重置。有什么办法可以避免这种情况。

import QtQuick 2.7
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello Quick")

    Rectangle{
        id: item_
        width: 300
        height: 300
        color: "red"

        MouseArea{
            id: drag_
            anchors.fill: parent

            drag.target: item_
        }

        states: State{
            when: drag_.drag.active
            PropertyChanges {
                target: item_

                x: 100
                y: 100
            }
        }

        onXChanged: console.log("item_.x: ", x)
    }
}

输出:

qml: item_.x:  100
qml: item_.x:  0
qml: item_.x:  1
qml: item_.x:  2
qml: item_.x:  3
qml: item_.x:  4
qml: item_.x:  5
qml: item_.x:  6
qml: item_.x:  7
qml: item_.x:  8
qml: item_.x:  10

0 个答案:

没有答案
相关问题