【发布时间】:2023-03-25 02:50:01
【问题描述】:
下面的应用程序输出从一个QQuickItem 映射到另一个的坐标。根据文档:
object mapToItem(Item item, real x, real y)
将位于此项的坐标系中的点 (x, y) 或 rect (x, y, width, height) 映射到项的坐标系,并返回一个 x 和 y(以及可选的宽度和高度)属性匹配的对象映射坐标。
我希望输出是这样的:
0 0
1 1
2 2
但我明白了:
QPointF(9, 6) 100
QPointF(9, 5) 100
QPointF(8, 5) 100
为什么mapped.x 的类型是QPointF?我希望它是一个浮点值。什么是实际的 mapped 类型?
import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Window 2.0
import QtQuick.Layouts 1.0
ApplicationWindow {
width: 640
height: 480
Rectangle {
anchors.fill: parent
id: r0
color: "green"
Rectangle {
x: 50; y:100; width: 100; height: 100;
id: r
color: "yellow"
MouseArea {
id: mousearea
anchors.fill: parent
hoverEnabled: true
onPositionChanged: {
var mouseP = Qt.point(mouse.x, mouse.y);
var mapped = r.mapToItem(r0, mouseP);
console.log(mapped.x, ' ', mapped.y);
}
}
}
}
}
UPDATE1: QQuickItem 有 mapToItem 有 2 个参数,Item 有 mapToItem 有 3 个参数。上面的代码调用了哪一个?
【问题讨论】:
-
我查看了
QQuickItem::mapToItem(QQmlV4Function *args),但我无法弄清楚为什么mapped.x的类型是QPointF。 -
有趣的问题,看我的更新。
标签: qt qml qtquick2 qquickitem