【发布时间】:2018-07-04 01:11:40
【问题描述】:
在下面的应用程序中,您可以单击场景中的任意位置以将“视图”置于该点的中心。
import QtQuick 2.7
import QtQuick.Controls 2.2
ApplicationWindow {
width: 640
height: 480
visible: true
property real zoom: 1
property point offset
property point scenePosToCentreOn
Item {
id: sceneView
anchors.fill: parent
MouseArea {
anchors.fill: parent
onClicked: {
scenePosToCentreOn = scene.mapFromItem(sceneView, mouse.x, mouse.y)
offset = Qt.point(scenePosToCentreOn.x - width / 2,
scenePosToCentreOn.y - height / 2);
}
onWheel: {
var zoomFactor = Math.pow(1.4, wheel.angleDelta.y / 120.0);
var newZoom = Math.min(8.0, Math.max(0.25, zoom * zoomFactor));
zoom = newZoom;
}
}
Item {
id: scene
implicitWidth: backgroundImage.implicitWidth
implicitHeight: backgroundImage.implicitHeight
transform: [
Translate {
x: -offset.x
y: -offset.y
},
Scale {
xScale: zoom
yScale: zoom
}
]
Image {
id: backgroundImage
source: "http://cdn.akamai.steamstatic.com/steam/apps/393010/ss_29cf93db42617dd08ceb0a0bf0a4b62ad12a1cfc.1920x1080.jpg?t=1459456906"
}
Rectangle {
x: scenePosToCentreOn.x - width / 2
y: scenePosToCentreOn.y - height / 2
width: 8
height: width
radius: width / 2
color: "#fff"
}
Rectangle {
anchors.fill: parent
color: "transparent"
border.color: "darkorange"
border.width: 4
Label {
text: "Scene"
}
}
}
Label {
text: zoom.toFixed(2)
font.pixelSize: Qt.application.font.pixelSize * 2
color: "salmon"
anchors.right: parent.right
anchors.bottom: parent.bottom
}
}
}
我希望能够放大和缩小所选点 (scenePosToCentreOn)。它目前在zoom 为1 时有效,但对于任何其他zoom 值,它似乎起源于屏幕的左上角。我怀疑我在转换列表中遗漏了一些东西,但我想不通。
【问题讨论】:
-
标签是相关的,请不要更改我的英式英语。 :) 我会看看那个答案,看看它是否有帮助。
-
为什么相机是相关的?
-
视图是场景中的相机。最初的用例是一个游戏,所以我想吸引有这方面经验的人。
-
概念上是的,但是这个元素在这种情况下是不合适的,最好使用像
qt这样更通用的标签,这样你会吸引更多可以帮助你的人。 -
好的,公平点。关于帮助的问题,我看不出你投票结束我的问题的答案是如何解决我的问题的。也许您愿意使用适合我的 sn-p 的代码发布答案? :)
标签: qt math qml transformation qtquick2