Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 长按后拖动_Qt_Qml_Drag_Qtquick2 - Fatal编程技术网

Qt 长按后拖动

Qt 长按后拖动,qt,qml,drag,qtquick2,Qt,Qml,Drag,Qtquick2,我想在长时间按下自定义按钮后拖动它们。我已经实现了这种行为,但问题是,在启用拖动后,我需要再次按下按钮以实际开始拖动。如果我想在长按后移动按钮而不释放,我应该如何实现这个机制 这是我的按钮代码(onReleased和onLongPressed是我自己的信号): 有什么想法吗?一般来说,您可以连接不同的信号,并按照本文讨论的方式连接操作。你应该看看它,因为它充满了美好和有用的信息 然而,当涉及到鼠标事件时,MouseEvents接受提供了一种有趣的事件串联方法。关于MouseEvent::acce

我想在长时间按下自定义按钮后拖动它们。我已经实现了这种行为,但问题是,在启用
拖动
后,我需要再次按下按钮以实际开始拖动。如果我想在长按后移动按钮而不释放,我应该如何实现这个机制

这是我的按钮代码(
onReleased
onLongPressed
是我自己的信号):


有什么想法吗?

一般来说,您可以连接不同的信号,并按照本文讨论的方式连接操作。你应该看看它,因为它充满了美好和有用的信息

然而,当涉及到鼠标事件时,
MouseEvent
s接受提供了一种有趣的事件串联方法。关于
MouseEvent::accepted

将accepted设置为true可防止鼠标事件被忽略 已传播到此项下的项。通常情况下,如果项目作用于 鼠标事件,然后它应该被接受,以便项目在 堆叠顺序也不响应同一事件

在这种情况下,我们可以采取相反的方法,不接受事件。通过这种方式,
pressed
事件可用于激活拖动并实际执行拖动。然后可以在拖动结束时发生的
release
事件中接受
MouseEvent

下面是一个遵循这种方法的简单示例。按住鼠标时,设置了拖动目标,可以开始拖动,而松开鼠标时,将删除拖动目标,从而消除拖动行为。要测试它,只需在矩形上按住鼠标,当它改变颜色时,拖动它即可

import QtQuick 2.4
import QtQuick.Controls 1.3

ApplicationWindow {
    width: 300
    height: 300
    visible: true

    Rectangle {
        id: item
        border.width: 2
        x: 100
        y: 100
        width: 100
        height: 100
        state: "BASE"

        states: [
            State {
            name: "BASE"
            PropertyChanges { target: mouseArea; drag.target: undefined}
            PropertyChanges { target: item; color: "steelblue"}
        },
            State {
            name: "DRAGGABLE"
            PropertyChanges { target: mouseArea; drag.target: item}
            PropertyChanges { target: item; color: "darkblue"}
        }
        ]


        MouseArea {
            id: mouseArea
            anchors.fill: parent
            drag{
                // target:  NOT SET HERE
                minimumX: 0
                minimumY: 0
                maximumX: parent.parent.width - parent.width
                maximumY: parent.parent.height - parent.height
                smoothed: true
            }

            onPressAndHold: {
                item.state = "DRAGGABLE"
                mouse.accepted = false      // mouse event is USED but NOT CONSUMED...
            }

            onReleased: {
                item.state = "BASE"         // mouse event acceptation occurs here!
            }
        }
    }
}

这种简单的方法也可以很好地用于自定义信号。

请注意,如果我们离开可拖动状态,属性将自动取消设置。没有必要把一切都恢复到基本状态。是的,是的。毫无用处的状态,真的。不知道我为什么加上它。
import QtQuick 2.4
import QtQuick.Controls 1.3

ApplicationWindow {
    width: 300
    height: 300
    visible: true

    Rectangle {
        id: item
        border.width: 2
        x: 100
        y: 100
        width: 100
        height: 100
        state: "BASE"

        states: [
            State {
            name: "BASE"
            PropertyChanges { target: mouseArea; drag.target: undefined}
            PropertyChanges { target: item; color: "steelblue"}
        },
            State {
            name: "DRAGGABLE"
            PropertyChanges { target: mouseArea; drag.target: item}
            PropertyChanges { target: item; color: "darkblue"}
        }
        ]


        MouseArea {
            id: mouseArea
            anchors.fill: parent
            drag{
                // target:  NOT SET HERE
                minimumX: 0
                minimumY: 0
                maximumX: parent.parent.width - parent.width
                maximumY: parent.parent.height - parent.height
                smoothed: true
            }

            onPressAndHold: {
                item.state = "DRAGGABLE"
                mouse.accepted = false      // mouse event is USED but NOT CONSUMED...
            }

            onReleased: {
                item.state = "BASE"         // mouse event acceptation occurs here!
            }
        }
    }
}