当父级的位置在拖动过程中变化时,如何在不忽视的情况下制作项目可拖动
我正在尝试制作一个可以通过其边缘调整大小的项目。
为了显示问题的最小测试柜,足以拥有左边缘可拖动,因此,这里是:
Rectangle {
id: root
border.width: 1
border.color: 'black'
color: 'red'
// save original position and size at drag start
property real origX: 0
property real origWidth: 0
// drag this item:
Item {
id: dragDummy
x: 0
onXChanged: {
root.x = root.origX + x
root.width = root.origWidth - x
}
}
MouseArea {
anchors.fill: root
drag.target: dragDummy
drag.axis: Drag.XAxis
drag.onActiveChanged: {
// onDragStarted -> Cannot assign to non-existent property "onDragStarted" ???
if(!active) return
root.origX = root.x
root.origWidth = root.width
}
}
}
问题似乎是,如果拖动导致父位置更改,则会触发另一个拖动事件,引起此闪烁:
我猜莫塞雷亚在这里无能为力吗?然后应像“老式”应用程序中使用低级别的鼠标事件(即在根项目中捕获事件,手动计算有关初始鼠标倒下位置等的偏移等等。)?
(或者我必须将Mousearea移到不会在阻力过程中移动的祖先,这几乎是一样的...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个不错的QML项目类型,称为
dragHandler
,人们经常忽略它,但我发现它效果很好。该解决方案比其他建议更惯用,因为它使用声明性的样式而不是命令:
There is a nice QML Item type called
DragHandler
which people often overlook, but I find that it works very well.This solution is a little more idiomatic than other suggestions in that it uses a declarative style rather than imperative:
我提出的解决方案包括有两个
mousearea
s:mousearea
与要拖动的项目移动,仅用于命中测试,因此其Onpressed
处理程序是这样的:mousearea
,堆叠在其他下方, 移动,处理鼠标位置更改和拖动:这可以拖动该项目可靠。
这也是我想要避免的,因为它重新发明了鼠标阻力,但是在没有更好的解决方案的情况下,我将使用的是。
我不会接受这个答案,因为我很想看到其他方法来解决这个问题。
The solution I come up with consists of having two
MouseArea
s:MouseArea
moves with the item to drag, that is used only for hit-testing, so itsonPressed
handler is something like this:MouseArea
, stacked below the others and not moving, handles the mouse position change and the dragging:This is able to drag the item reliably.
This is also what I wanted to avoid, because it reinvents mouse drag, but in absence of a better solution it is what I am going to use.
I won't accept this answer as I'm curious to see other ways to approach this problem.
您可以通过用 item 的新移动和新定位加入。 html#maptoItem-method“ rel =“ nofollow noreferrer”> maptoItem 函数。
在我的解决方案中,我没有使用
mousearea
的阻力功能,因为它需要drag.target
。我使用了压制和位置更改信号来实现阻力行为。唯一的缺点是背景
item
是maptoItem函数所需的,因为它不接受窗口
,因为它不是<代码>项目。You can workaround the movement and new positioning of the dragged
Item
by mapping the coordinates with the mapToItem functions.In my solution, I've not used the drag functionality of the
MouseArea
as it needs adrag.target
. I've used the pressed and position changed signals to implement drag behavior. The only downside is thebackground
Item
which is needed for the mapToItem function as it doesn't accept theWindow
due to it not being anItem
.