获取动态创建的Component坐标
Obtain coordinates of dynamically created Component
我有一个主放置区域,加载时动态创建一个新的矩形组件。新建的矩形组件可以在拖拽区域内拖拽。但是,当矩形为dragged/dropped.
时,我不知道如何获取拖动区域上新矩形组件的坐标。
编辑
我不知何故需要空降区代码中的新坐标数据
拖放区代码
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
}
}
}
我只知道一种获取动态创建对象的方法,该对象未附加到任何变量或 属性 - 您需要通过名称或已知索引在 data
属性 它的父项。
例如。你的 Component.onCreated
会变成这样:
Component.onCompleted: {
var dynamicRectangle2 = Qt.createComponent("Test2.qml");
dynamicRectangle2.createObject(parent, {x:100, y: 100, objectName: "dynamic_rectangle"})
var index = indexOfObject("dynamic_rectangle", parent)
if (index == -1)
console.debug("couldn't find an object")
else
console.debug("object found at: (" + parent.data[index].x + "," + parent.data[index].y + ")")
}
函数indexOfObject
:
// Searches for object with objectName inside parent's property 'data'
// @param objName object name to search for
// @param parent object where to search
// @return -1 if not found, index if found
function indexOfObject(objName, parent) {
for (var i = 0 ; i < parent.data.length; i++) {
if (parent.data[i].objectName === objName)
return i
}
return -1
}
我已经使用 signals and slots
解决了这个问题。我使用
将新矩形中鼠标操作发出的信号连接到主区域中提到的插槽
// This creates the new rectangle component
Component.onCompleted: {
var dynamicRectangle2 = Qt.createComponent("Test2.qml");
// Assign new variable to the created object
// Use this variable to connect the signals and slots
var newRect = dynamicRectangle2.createObject(root, {x:100, y: 100})
newRect.dragged.connect(onDropAreaEntered)
newRect.dropped.connect(onDropAreaDropped)
}
这里是完整的代码
放置区
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.3
Page{
id: root
// On Dropped
function onDropAreaDropped(x,y){
console.log("Dropped" , x, y)
}
// On Entered
function onDropAreaEntered(x,y){
console.log("Dragging" , x, y)
}
// 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");
// Assign new variable to the created object
// Use this variable to connect the signals and slots
var newRect = dynamicRectangle2.createObject(root, {x:100, y: 100})
newRect.dragged.connect(onDropAreaEntered)
newRect.dropped.connect(onDropAreaDropped)
}
}
}
Test2.qml
的代码 - 矩形组件
import QtQuick 2.15
import QtQuick.Controls 2.15
Rectangle{
id:dragRect
height: 40
width: 60
color: "blue"
signal dragged(double x, double y);
signal dropped(double x, double y);
MouseArea{
id: mArea
anchors.fill: parent
drag.target: dragRect
onReleased: {
dragRect.dropped(dragRect.x, dragRect.y)
}
onPositionChanged: {
dragRect.dragged(dragRect.x, dragRect.y)
}
}
}
我有一个主放置区域,加载时动态创建一个新的矩形组件。新建的矩形组件可以在拖拽区域内拖拽。但是,当矩形为dragged/dropped.
时,我不知道如何获取拖动区域上新矩形组件的坐标。编辑 我不知何故需要空降区代码中的新坐标数据
拖放区代码
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
}
}
}
我只知道一种获取动态创建对象的方法,该对象未附加到任何变量或 属性 - 您需要通过名称或已知索引在 data
属性 它的父项。
例如。你的 Component.onCreated
会变成这样:
Component.onCompleted: {
var dynamicRectangle2 = Qt.createComponent("Test2.qml");
dynamicRectangle2.createObject(parent, {x:100, y: 100, objectName: "dynamic_rectangle"})
var index = indexOfObject("dynamic_rectangle", parent)
if (index == -1)
console.debug("couldn't find an object")
else
console.debug("object found at: (" + parent.data[index].x + "," + parent.data[index].y + ")")
}
函数indexOfObject
:
// Searches for object with objectName inside parent's property 'data'
// @param objName object name to search for
// @param parent object where to search
// @return -1 if not found, index if found
function indexOfObject(objName, parent) {
for (var i = 0 ; i < parent.data.length; i++) {
if (parent.data[i].objectName === objName)
return i
}
return -1
}
我已经使用 signals and slots
解决了这个问题。我使用
// This creates the new rectangle component
Component.onCompleted: {
var dynamicRectangle2 = Qt.createComponent("Test2.qml");
// Assign new variable to the created object
// Use this variable to connect the signals and slots
var newRect = dynamicRectangle2.createObject(root, {x:100, y: 100})
newRect.dragged.connect(onDropAreaEntered)
newRect.dropped.connect(onDropAreaDropped)
}
这里是完整的代码
放置区
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.3
Page{
id: root
// On Dropped
function onDropAreaDropped(x,y){
console.log("Dropped" , x, y)
}
// On Entered
function onDropAreaEntered(x,y){
console.log("Dragging" , x, y)
}
// 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");
// Assign new variable to the created object
// Use this variable to connect the signals and slots
var newRect = dynamicRectangle2.createObject(root, {x:100, y: 100})
newRect.dragged.connect(onDropAreaEntered)
newRect.dropped.connect(onDropAreaDropped)
}
}
}
Test2.qml
的代码 - 矩形组件
import QtQuick 2.15
import QtQuick.Controls 2.15
Rectangle{
id:dragRect
height: 40
width: 60
color: "blue"
signal dragged(double x, double y);
signal dropped(double x, double y);
MouseArea{
id: mArea
anchors.fill: parent
drag.target: dragRect
onReleased: {
dragRect.dropped(dragRect.x, dragRect.y)
}
onPositionChanged: {
dragRect.dragged(dragRect.x, dragRect.y)
}
}
}