Hi all!
I’d like to write my very first message in this forum since the acquisition by Digia and creation of the Qt Project.
My problem is that Item::mapToItem(null, x, y) is giving wrong values. As I’m a beginner about the new Qt Quick 2, and QML in general, it is most probably due to a misuse but I cannot find it.
A maybe-not-so-minimal working example to show my case follows. It creates a couple of rectangles with a Repeater, and fails at finding the central point for the second one (stored in posX, posY properties). Being squares of size 25, I’d expect the second one to stay at (0, 25), with its center at (0, 37.5), but mapToItem() says that it is placed at (0, 50), so the center calculations go for (0, 67.5) instead.
I would bet the culprit is the reparenting done to the items, but that’s the only way I found to have a clean list of Rectangles: if the Repeater was placed inside the Column, as most Qt examples show, then the Column’s children would include that Repeater, which is not nice for code abstraction (other parts of the code shouldn’t have to deal with that detail).
import QtQuick 2.0
Item {
id: root
width: buttonSize
height: (2 * buttonCount + 1) * buttonSize
property int buttonCount: 2
property real buttonSize: 25
property alias buttons: container.children
Component.onCompleted: {
console.log("root: onCompleted")
for (var i = 0; i < buttons.length; ++i) {
console.log("root:", buttons[i].buttonId, "x", buttons[i].x, "y", buttons[i].y)
buttons[i].posUpdate()
}
}
Column {
id: container
anchors {
fill: parent
// topMargin: buttonSize
}
// spacing: buttonSize
}
Repeater {
model: buttonCount
Item {
// Use a dummy Item to wrap our Socket element.
// The wrapper will become child of the Repeater's parent,
// while the Socket will be reparented to our container.
id: wrapper
Rectangle {
id: button
width: buttonSize; height: buttonSize; radius: buttonSize
color: "red"
property string buttonId: "button_" + index
property real posX
property real posY
function posUpdate() {
console.log("button: posUpdate", buttonId, "button.x", x, "button.y", y, "mapToItem().x", mapToItem(null, x, y).x, "mapToItem().y", mapToItem(null, x, y).y)
posX = button.mapToItem(null, x, y).x + (buttonSize / 2.0)
posY = button.mapToItem(null, x, y).y + (buttonSize / 2.0)
console.log("button: posUpdate", buttonId, "posX", posX, "posY", posY)
}
Component.onCompleted: {
parent = container
}
}
}
}
}
Any help would be appreciated :-)
↧