为什么没有onReleased在一次阻力之后开火

时间:2015-08-31 20:52:21

标签: qt qml qtquick2

我有一个像这样的鼠标区域

onPressed: {
  cursorShape = Qt.ClosedHandCursor
  console.log("closed")
}

onReleased: {
  cursorShape = Qt.OpenHandCursor
  console.log("open")
}

cursorShape: Qt.OpenHandCursor

如果我在没有任何鼠标移动的情况下单击并释放,则光标图标会按预期更改。如果我在点击的同时移动鼠标,光标图标仍然是一个闭合的手。

为什么?

1 个答案:

答案 0 :(得分:2)

我刚刚使用QML中的以下代码检查了行为是否正确。

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2

ApplicationWindow {
    id: mainwindow
    width: 640
    height: 480
    visible: true

    Flickable {
        width: 200; height: 200
        contentWidth: image.width
        contentHeight: image.height
        clip: true

        Image {
            id: image;
            source: "images/icon.png"

            MouseArea {
                anchors.fill: parent
                preventStealing: true

                onPressed: {
                  cursorShape = Qt.ClosedHandCursor
                  console.log("closed")
                }

                onReleased: {
                  cursorShape = Qt.OpenHandCursor
                  console.log("open")
                }

                cursorShape: Qt.OpenHandCursor
            }
        }
    }
}

preventStealing设为true可使MouseArea正常工作,cursorShape更改正常。但缺点(一个很大的缺点)是MouseArea偷走了轻弹手势,因此不会产生轻弹动作。

因此,我建议在用户互动结束时处理onMovementEnded以设置cursorShape = Qt.OpenHandCursor

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2

ApplicationWindow {
    id: mainwindow
    width: 640
    height: 480
    visible: true

    Flickable {
        width: 200; height: 200
        contentWidth: image.width
        contentHeight: image.height
        clip: true

        onMovementEnded: {
            mousearea.cursorShape = Qt.OpenHandCursor
            console.log("onMovementEnded")
        }

        Image {
            id: image;
            source: "images/icon.png"

            MouseArea {
                id: mousearea
                anchors.fill: parent

                onPressed: {
                    cursorShape = Qt.ClosedHandCursor
                    console.log("closed")
                }

                onReleased: {
                    cursorShape = Qt.OpenHandCursor
                    console.log("open")
                }

                cursorShape: Qt.OpenHandCursor
            }
        }
    }
}