Qt 如何将QQuickItem的中心转换为新中心
我有一个Qt-QML应用程序。以下是应用程序的完整代码: 代码: 问题: 我的问题很简单。如何将任何QQuickitem的中心更改为新中心?因此,在上述情况下,当我的项目2获得单击事件时,如何将我的项目1的中心更改为新的中心Qt.pointsome_x_center,some_y_center。 此外,是否可以获得另一个项目的中心?像应用程序\主\窗口或我的\项\ 2的中心应用到目标我的\项\ 1 附言:Qt 如何将QQuickItem的中心转换为新中心,qt,qml,qtquick2,qquickitem,Qt,Qml,Qtquick2,Qquickitem,我有一个Qt-QML应用程序。以下是应用程序的完整代码: 代码: 问题: 我的问题很简单。如何将任何QQuickitem的中心更改为新中心?因此,在上述情况下,当我的项目2获得单击事件时,如何将我的项目1的中心更改为新的中心Qt.pointsome_x_center,some_y_center。 此外,是否可以获得另一个项目的中心?像应用程序\主\窗口或我的\项\ 2的中心应用到目标我的\项\ 1 附言: 我已经使代码简单化,以使问题客观化。在我的实际代码中,我有一个相当复杂的逻辑,我需要在不使
我已经使代码简单化,以使问题客观化。在我的实际代码中,我有一个相当复杂的逻辑,我需要在不使用锚的情况下将像我的_item _1这样的东西重新对齐到一个新的中心,因为我尝试使用的QQuickitem被缩放并平移到一个新的点。如果不能使用锚,你必须手动计算中心并分配它 快速示例:
Item
{
anchors.fill: parent
Rectangle
{
id: nonParentOrSibling
width: 200
height: 200
x: 350
y: 240
color: "red"
}
}
Rectangle
{
id: rectToMove
width: 100
height: 100
y: 200
color: "blue"
MouseArea
{
anchors.fill: parent
onClicked:
{
var itemCenter = Qt.point(nonParentOrSibling.x + nonParentOrSibling.width / 2, nonParentOrSibling.y + nonParentOrSibling.height / 2)
rectToMove.x = itemCenter.x - rectToMove.width / 2
rectToMove.y = itemCenter.y - rectToMove.height / 2
}
}
}
请注意,这只是一次移动,如果移动目标,矩形将不会移动
为了让它跟随目标,你必须重新绑定它
如果rectToMove与nonParentOrSibling不在同一坐标系中,则可以使用Item.mapToItem和mapFromItem来调整坐标。如果在QML中讨论,请查看anchors属性。对于实现,可以使用MouseArea和onclick以及my_item_1.anchors.centerIn:app_主窗口。这就是你要找的吗?anchors.centerIn有什么问题吗?@dydil一个潜在的问题是你只能使用带有锚的父母或兄弟姐妹。@dydil如我所说。我有一个不同的问题可以解决,如果我可以改变我的项目1的中心。此示例只有足够的代码来演示问题。确定。我必须手动计算项目中心。这是我所期待的。谢谢你的回答。试一试
Item
{
anchors.fill: parent
Rectangle
{
id: nonParentOrSibling
width: 200
height: 200
x: 350
y: 240
color: "red"
}
}
Rectangle
{
id: rectToMove
width: 100
height: 100
y: 200
color: "blue"
MouseArea
{
anchors.fill: parent
onClicked:
{
var itemCenter = Qt.point(nonParentOrSibling.x + nonParentOrSibling.width / 2, nonParentOrSibling.y + nonParentOrSibling.height / 2)
rectToMove.x = itemCenter.x - rectToMove.width / 2
rectToMove.y = itemCenter.y - rectToMove.height / 2
}
}
}