QML-右键单击MouseArea中未检测到

时间:2014-03-16 22:01:10

标签: qt qml qtquick2

我正在使用图像编辑器,专门使用MouseArea(在Button类型中)设置当前左或右按钮的相关颜色。我遇到的问题是我似乎根本无法过滤特定的按钮。这是给我带来麻烦的片段:

    Button {
        x: 60
        width: 80
        height: 40
        text: "Blue"

        anchors.verticalCenter: parent.verticalCenter

        onButtonClick: {
            if(mouseArea.pressedButtons & Qt.RightButton) {
                console.log("Right button used");
                GlobalState.setRightColorValues(0.0, 0.0, 1.0, 1.0);
            } else {
                console.log("Left button used");
                GlobalState.setLeftColorValues(0.0, 0.0, 1.0, 1.0);
            }
        }
    }

(如果需要,我可以提供整个Button.qml,但它主要来自here)。

我试图关注示例here,但用于过滤鼠标右键单击的方法似乎不起作用(无论如何)。会发生什么声明"默认"假设左键单击。我也尝试将两者分成不同的if语句,但这样做会导致 no 按钮被明确过滤。

为了过滤特定的鼠标按钮需要更改什么?或者我是否必须实现"切换原色" Paint / Paint.NET中使用的按钮?

编辑1:我已经意识到Button.qml中缺少相关的代码段 -

 MouseArea{
    id: buttonMouseArea;

    acceptedButtons: Qt.AllButtons;
    hoverEnabled: true

    onEntered: parent.color = onHoverColor
    onExited:  parent.color = buttonColor

    anchors.fill: parent;

    onClicked: buttonClick();
}

这嵌套在Rectangle内,该Text也包含{{1}}字段。

5 个答案:

答案 0 :(得分:17)

默认情况下,MouseArea仅处理鼠标左键。您可以通过设置acceptedButtons属性来处理其他按钮

MouseArea {
   acceptedButtons: Qt.LeftButton | Qt.RightButton
}

请参阅http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-mousearea.html#acceptedButtons-prop

答案 1 :(得分:12)

您可以通过以下方式检查按钮:

MouseArea {
   acceptedButtons: Qt.LeftButton | Qt.RightButton
   onClicked: {
        if(mouse.button & Qt.RightButton) {
        }
    }
}

答案 2 :(得分:2)

我观察到pressedButtons仅适用于onPressed,而不适用于onClicked
我觉得有点奇怪,因为clicked()只不过是a press followed by a release。因此,我认为它也适用于clicked(),但遗憾的是它没有。

示例:

MouseArea {
    id: idMouseArea

    acceptedButtons: Qt.LeftButton | Qt.RightButton

    anchors.fill: parent

    //onClicked: {   // pressedButtons not identified with onClicked
    onPressed: {     // pressedButtons identified and works well with onPressed
        if (idMouseArea.pressedButtons & Qt.RightButton) {
            console.log("right-button pressed")
        } else if (idMouseArea.pressedButtons & Qt.LeftButton) {
            console.log("left-button pressed")
        }
    }
}

这是我在Qt 5.5.1和QtQuick 2.5中观察到的。 documentation并未显示如何将pressedButtons属性与if-else一起使用。如果观察错误,请更正/评论。

<强>更新
如果您迫切需要将pressedButtonsonClicked一起使用,则可以使用以下黑客方法来执行此操作。

    MouseArea {
        property int mouseButtonClicked: Qt.NoButton

        acceptedButtons: Qt.RightButton | Qt.LeftButton

        anchors.fill: parent

        onPressed: {
            if (pressedButtons & Qt.LeftButton) {
                mouseButtonClicked = Qt.LeftButton
            } else if (pressedButtons & Qt.RightButton) {
                mouseButtonClicked = Qt.RightButton
            }
        }

        onClicked: {
            if (mouseButtonClicked === Qt.LeftButton) {
                console.log("left button clicked")
            } else if (mouseButtonClicked === Qt.RightButton) {
                console.log("right button clicked")
            }
        }
    }

答案 3 :(得分:0)

由于您正在从onClicked处理程序生成buttonClick()方法,因此不应在条件中包含pressedButtons()。因此,将您的条件改为

if (Qt.RightButton) { ... }

答案 4 :(得分:0)

如果您为感兴趣的每个鼠标按钮指定一个if,则可以避免使用MouseArea语句:

MouseArea {
    anchors.fill: parent
    onClicked: console.log("do left click action")
}

MouseArea {
    anchors.fill: parent
    acceptedButtons: Qt.RightButton
    onClicked: console.log("do right click action")
}
相关问题