【问题标题】:QML transfer mouse ownership dynamically while mouse pressedQML 在鼠标按下时动态转移鼠标所有权
【发布时间】:2019-09-12 14:49:43
【问题描述】:

我有一个应用程序,右侧有一个图像列表(使用 ListView) 和左侧的 查看器。用户可以将图像从列表拖到查看器中,并将图像保留在列表中(类似于列表的预览,但具有拖放功能)。

为此,当用户在列表中的图像上“pressAndHold”时,我会创建该图像的副本并将其放在列表中的图像前面(我更改了边框,所以我知道它是副本之一)。

如果我随后释放并再次单击副本,我可以将副本移动到查看器,一旦我释放副本,我会销毁副本并处理拖放到查看器区域。

除非我释放并单击副本,否则我无法执行此操作,因为当鼠标处于按住状态时,我无法将“鼠标所有权”从列表图像鼠标区域转移到复制图像鼠标区域。

有什么想法吗?提前致谢!

【问题讨论】:

    标签: qt drag-and-drop qml focus mouseevent


    【解决方案1】:

    对于任何寻找类似东西的人来说,这就是我的做法: 在委托上,我添加了鼠标区域:

    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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多