QML-鼠标区未检测到右键



我正在研究一个图像编辑器,特别是使用MouseArea(在按钮类型内)设置当前的左或右按钮的相关颜色。我遇到的问题是,我似乎根本无法过滤特定的按钮。下面是给我带来麻烦的代码片段:

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

(如果需要,我可以提供完整的按钮。Qml,但主要来自这里)。

我试图遵循这里的例子,但用于过滤鼠标右键的方法似乎不起作用(无论如何)。所发生的情况是,语句"默认"假设单击左键。我也试过将这两个按钮分成不同的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字段。

默认情况下,MouseArea只处理鼠标左键。您可以通过设置acceptedButtons属性来处理其他按钮。你可以通过在onClicked处理程序中访问mouse MouseEvent来确定哪个按钮引起了点击。

MouseArea {
    acceptedButtons: Qt.LeftButton | Qt.RightButton
    onClicked: {
        if (mouse.button === Qt.RightButton) { // 'mouse' is a MouseEvent argument passed into the onClicked signal handler
            console.log("right button clicked!")
        } else if (mouse.button === Qt.LeftButton) {
            console.log("left button clicked!")
        }
    }
}

参见acceptedButtons和mouse MouseEvent

您可以这样检查按钮:

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

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

import QtQuick
import QtQuick.Controls
Page {
    Rectangle {
        anchors.centerIn: parent
        color: "#ffe"
        border.color: "grey"
        width: parent.width / 2
        height: parent.height / 2
        Text {
            id: txt
            anchors.centerIn: parent
            text: "Use Mouse here"
        }
        MouseArea {
           anchors.fill: parent
           onClicked: txt.text = "left click detected";
        }
        MouseArea {
            anchors.fill: parent
            acceptedButtons: Qt.RightButton
            onClicked: txt.text = "right click detected";
        }
    }
}

你可以在网上试试!

我观察到的是pressedButtons只适用于onPressed,而不适用于onClicked
我觉得这有点奇怪,因为clicked()只不过是一个新闻稿之后的新闻稿。因此,我认为它也可以与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中观察到的。文档没有显示如何使用pressedButtons属性与if-else。如果观察错误,请纠正/评论。


更新:
如果你确实需要使用pressedButtonsonClicked,你可以使用下面的hack来做到这一点。

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

最新更新