如何在QML中模仿按钮

时间:2012-03-18 16:17:52

标签: qt qml

我想在QML中模仿Button,但为什么它只适用于containsMouse!containsMouse,它不适用于OnPressed。你能帮助我吗 ?谢谢。

   Item {
        id: area
        property string str_noPressed;
        property string str_hover;
        property string str_pressed;

        Image {
            id: background
            width: 75; height: 75
            source: userArea.containsMouse ? str_hover : str_noPressed

            MouseArea {
                id: userArea
                anchors.fill: parent
                onClicked: {}
                onPressed: background.source = str_pressed
                hoverEnabled: true
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

显然,在按下按钮时,它还包含鼠标。

我认为containsMouse优先于onPressed。尝试:

onContainsMouseChanged:  {
    if (containsMouse && !pressed) {
        background.source = str_pressed
    }
}