简体   繁体   中英

Adjust translation speed of QML DragHandler

my question is about using a QML DragHandler to move a QML Item. I have successfully implemented position through dragging (when holding the Ctrl modifier) like so:

DragHandler {
    dragThreshold: 0
    acceptedModifiers: Qt.ControlModifier
}

Now I would like to add another handler that allows me to precisely position the element. Other software does this throught the use of the shift modifier. So what I want to do is move the element not by the pixel amount that the mouse moves, but an amount smaller than that. Ideally I would want to do something like this:

DragHandler {
    dragThreshold: 0
    acceptedModifiers: Qt.ShiftModifier

    onActiveTranslationChanged: {
        activeTranslation *= 0.5;
    }
}

Unfortunatelly activeTranslation is read-only and I don't see any other property I could use and I can't think of any other way to do it... Does anybody have an idea?

Thank you very much in advance!

Unfortunately Qt doesn't provide any way to change the drag speed AFAIK.

But this is a way to achieve it:

Rectangle
{
    id: theDraggableElement
    width: 100
    height: width
    color: "red"

    DragHandler
    {
        id: dragHandlerFast
        dragThreshold: 0
        acceptedModifiers: Qt.ControlModifier
        target: theDraggableElement
    }
}

Item
{
    id: invisibleItemForSlowDragging
    width: theDraggableElement.width
    height: theDraggableElement.height

    Binding { restoreMode: Binding.RestoreBinding; when: !dragHandlerSlow.active; target: invisibleItemForSlowDragging; property: "x"; value: theDraggableElement.x }
    Binding { restoreMode: Binding.RestoreBinding; when: !dragHandlerSlow.active; target: invisibleItemForSlowDragging; property: "y"; value: theDraggableElement.y }

    DragHandler
    {
        id: dragHandlerSlow
        dragThreshold: 0
        acceptedModifiers: Qt.ShiftModifier
        target: invisibleItemForSlowDragging

        onTranslationChanged:
        {
            theDraggableElement.x = invisibleItemForSlowDragging.x - dragHandlerSlow.translation.x / 2
            theDraggableElement.y = invisibleItemForSlowDragging.y - dragHandlerSlow.translation.y / 2
        }
    }
}

I have tested this with Qt 5.15.2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM