ChildAt() in QML , a question .i need help,thank you!
-
Please, use correct interpunction, your post is very hard to read.
If you don't know how to use this at all, read the docs. If you have any specific problem, please specify what is wrong.
-
Perhaps this is what you want,
@
import QtQuick 2.0
import QtQuick.Controls 1.0Rectangle {
width: 800
height: 480
Text {
text: qsTr("Drag the two labels")
anchors.centerIn: parent
}Label { id: l1 text: "Label 1" x: 50 y: 50 width: 100 height: 100 color: "red" MouseArea { anchors.fill: parent property variant previousPosition onPressed: { previousPosition = Qt.point(mouseX, mouseY) } onPositionChanged: { if (pressedButtons == Qt.LeftButton) { var dx = mouseX - previousPosition.x var dy = mouseY - previousPosition.y l1.x = l1.x + dx; l1.y = l1.y + dy; } } } } Label { id: l2 text: "Label 2" x: 100 y: 100 width: 100 height: 100 color: "orange" MouseArea { anchors.fill: parent property variant previousPosition onPressed: { previousPosition = Qt.point(mouseX, mouseY) } onPositionChanged: { if (pressedButtons == Qt.LeftButton) { var dx = mouseX - previousPosition.x var dy = mouseY - previousPosition.y l2.x = l2.x + dx; l2.y = l2.y + dy; } } } }
}
@
-
Sorry but i didn't get you. In your first post you said you want to click the label and and move it.
How would you get the X an Y coordinates of the item unless you click it or you know in advance the position of each label ?
Can you elaborate more your problem ? -
hi,p3c0.
thank you for your help.
in my program. i defined 7 lables.Mousearea{
anchors.fill:parent
onClicked:{
var click;
click = mapToItem(parent,mouseX,mouseY);
parent.childAt(click.x,click.y).anchors.leftMargin = 50;
...
}
}like these,i want capture a lable by (mouseX,mouseY),but ,in running,
my program return wrong value. -
Hi,
Considering your requirement i implemented the following and it gives the item (its objectName )when clicked on it,
@
import QtQuick 2.0
import QtQuick.Controls 1.0Rectangle {
id: rect
width: 800
height: 480
Text {
text: qsTr("Click on each item")
anchors.centerIn: parent
}MouseArea { anchors.fill: parent onClicked: { var click; click = mapToItem(parent,mouseX,mouseY) var obj = parent.childAt(mouseX,mouseY) console.log(obj.objectName) } } Label { id: l1 objectName: "l1" text: "Label 1" x: 50 y: 50 width: 100 height: 100 color: "red" } Label { id: l2 objectName: "l2" text: "Label 2" x: 100 y: 100 width: 100 height: 100 color: "orange" } Rectangle { objectName: "r2" width: 200 height: 200 x: 500 y: 100 color: "red" }
}
@The MouseArea code is similar to your's but i moved it to the top before adding the two labels and the rect. If i move the MouseArea code after creation of labels and the rect it gives QQuickMouseArea as the child.
Hope this is what you want.