【发布时间】:2018-02-19 21:22:44
【问题描述】:
我有一个 Qt QML 应用程序。以下是应用程序的完整代码:
代码:
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
id: app_main_window
visible: true
width: 800
height: 600
title: qsTr("Hello QML")
Rectangle {
id: my_item_1
x: 100
y: 100
width: 100
height: 100
color: "grey"
visible: true
z: 1
}
Rectangle {
id: my_item_2
x: 400
y: 400
width: 100
height: 100
color: "grey"
visible: true
z: 1
MouseArea {
anchors.fill: parent
onClicked: {
// How can I change the center of my_item_1 to some arbitrary new center. lets say app_main_window's center. this involves another question. how can I get app_main_window's center?
}
}
}
}
问题:
我的问题很简单。 如何可以将任何QQuickitem 的中心 更改为新中心?因此,在上述情况下,当my_item_2 获得点击事件时,如何 将my_item_1 的中心更改为新中心(Qt.point(some_x_center, some_y_center))。
另外,是否有可能获得另一个项目的中心?点赞app_main_window或my_item_2的中心来申请目标my_item_1?
PS:
我已经简化了代码以使问题变得客观。我的实际代码中有一个相当复杂的逻辑,我需要在不使用锚点的情况下将my_item_1 之类的东西重新对齐到一个新的中心,因为我试图这样做的QQuickitem 被缩放并平移到一个新点。
【问题讨论】:
-
如果您在 QML 中讨论,请查看
anchors属性。对于实现,您将使用MouseArea和onClicked以及my_item_1.anchors.centerIn: app_main_window。这是你要找的吗? -
anchors.centerIn有什么问题? -
@dydil 一个潜在的问题是你只能使用带锚的父级或兄弟级。
-
@dydil 正如我所说。如果我可以更改
my_item_1的中心,我有一个不同的问题可以解决。此示例只有代码足以说明问题。
标签: qt qml qtquick2 qquickitem