【发布时间】:2020-08-18 16:45:30
【问题描述】:
我有一个主放置区域,加载时会动态创建一个新的矩形组件。新创建的矩形组件可以在拖动区域内拖动。但是,我不知道如何在拖放矩形时获取新矩形组件在拖动区域上的坐标。
编辑 我不知何故需要放置区代码中的新坐标数据
放置区代码
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.3
Page{
id: page1
// On Dropped
function onDropAreaDropped(drag){
console.log(JSON.stringify(drag))
}
// On Entered
function onDropAreaEntered(drag){
console.log(JSON.stringify(drag))
}
// This is the Drop area code
Rectangle{
id: dropRectangle
color: "beige"
width: parent.width
height: parent.height
DropArea {
id: dropArea
anchors.fill: parent
onEntered: onDropAreaEntered(drag)
onDropped: onDropAreaDropped(drag)
}
// This creates the new rectangle component
Component.onCompleted: {
var dynamicRectangle2 = Qt.createComponent("Test2.qml");
dynamicRectangle2.createObject(parent, {x:100, y: 100})
}
}
}
Test2.qml 的代码 - 矩形组件
import QtQuick 2.15
import QtQuick.Controls 2.15
Page {
id : somepageid
Rectangle{
id:dragRect
height: 40
width: 60
color: "blue"
// Need this x and y coordinate data in the drop area component
onXChanged: {
console.log(dragRect.x)
}
onYChanged: {
console.log(dragRect.y)
}
MouseArea{
id: mArea
anchors.fill: parent
drag.target: dragRect
}
}
}
【问题讨论】:
标签: qt drag-and-drop qml