QML 在按下鼠标时动态转移鼠标所有权
QML transfer mouse ownership dynamically while mouse pressed
我有一个应用程序,右侧有 图片列表 (使用
一个 ListView) 和一个 viewer 在左边。用户可以将图像从列表拖到查看器,并将图像保留在列表中(类似于列表预览,但具有拖放功能)。
为此,当用户 "pressAndHold" 在列表中的图像上时,我创建了该图像的副本并将其放在列表中的图像前面(我更改了边框所以我知道这是副本)。
如果我随后释放并再次单击副本,我可以将副本移动到查看器,一旦我释放副本,我会销毁副本并处理掉落在查看器区域上的掉落。
除非我松开并单击副本,否则我无法执行此操作,因为当鼠标处于暂停状态时,我无法将 "mouse ownership" 从列表图像鼠标区域转移到复制图像鼠标区域。
有什么想法吗?提前致谢!
任何寻找类似东西的人,这就是我的做法:
在委托上,我添加了鼠标区域:
MouseArea { // This is the mouse area on the original image-list
id: thumbnailDelegateMA
anchors { fill: parent }
cursorShape: containsMouse ? (drag.active ? Qt.ClosedHandCursor : Qt.PointingHandCursor) : Qt.ArrowCursor
property var copyThumbnail: undefined
drag.target: copyThumbnail ? copyThumbnail : null
onPressAndHold: {
if(!copyThumbnail){
copyThumbnail = createThumbnailCopy(imageID, parent)
parent = copyThumbnail
anchors.fill = copyThumbnail
}
}
onReleased:{
if(copyThumbnail){
parent = copyThumbnail.original
anchors.fill = copyThumbnail.original
copyThumbnail.destroy()
}
}
}
其中:
function createThumbnailCopy(uid, cparent){
var main_window = cparent.parent.parent;
var mapPos = mapFromItem(main_window, cparent.x, cparent.y);
var thumbnailCopy = thumbnailCopyComponent.createObject(
main_window,
{ "original": cparent,
"id": uid
"x": mapPos .x,
"y": mapPos .y
});
return thumbnailCopy;
}
并且:
Component{
id: thumbnailCopyComponent
Image {
id: thumbnailCopy
property string id;
property var original: undefined
Drag.hotSpot: Qt.point(width/2, 0)
Drag.active: true
fillMode: Image.PreserveAspectFit
source: "image://thumbnailProvider/" + id
}
}
我有一个应用程序,右侧有 图片列表 (使用 一个 ListView) 和一个 viewer 在左边。用户可以将图像从列表拖到查看器,并将图像保留在列表中(类似于列表预览,但具有拖放功能)。
为此,当用户 "pressAndHold" 在列表中的图像上时,我创建了该图像的副本并将其放在列表中的图像前面(我更改了边框所以我知道这是副本)。
如果我随后释放并再次单击副本,我可以将副本移动到查看器,一旦我释放副本,我会销毁副本并处理掉落在查看器区域上的掉落。
除非我松开并单击副本,否则我无法执行此操作,因为当鼠标处于暂停状态时,我无法将 "mouse ownership" 从列表图像鼠标区域转移到复制图像鼠标区域。
有什么想法吗?提前致谢!
任何寻找类似东西的人,这就是我的做法: 在委托上,我添加了鼠标区域:
MouseArea { // This is the mouse area on the original image-list
id: thumbnailDelegateMA
anchors { fill: parent }
cursorShape: containsMouse ? (drag.active ? Qt.ClosedHandCursor : Qt.PointingHandCursor) : Qt.ArrowCursor
property var copyThumbnail: undefined
drag.target: copyThumbnail ? copyThumbnail : null
onPressAndHold: {
if(!copyThumbnail){
copyThumbnail = createThumbnailCopy(imageID, parent)
parent = copyThumbnail
anchors.fill = copyThumbnail
}
}
onReleased:{
if(copyThumbnail){
parent = copyThumbnail.original
anchors.fill = copyThumbnail.original
copyThumbnail.destroy()
}
}
}
其中:
function createThumbnailCopy(uid, cparent){
var main_window = cparent.parent.parent;
var mapPos = mapFromItem(main_window, cparent.x, cparent.y);
var thumbnailCopy = thumbnailCopyComponent.createObject(
main_window,
{ "original": cparent,
"id": uid
"x": mapPos .x,
"y": mapPos .y
});
return thumbnailCopy;
}
并且:
Component{
id: thumbnailCopyComponent
Image {
id: thumbnailCopy
property string id;
property var original: undefined
Drag.hotSpot: Qt.point(width/2, 0)
Drag.active: true
fillMode: Image.PreserveAspectFit
source: "image://thumbnailProvider/" + id
}
}