1

我需要更改Button它按下时的背景图像。如果我在Button按下之前有背景图像,我需要在按下时更改该背景图像,然后在释放Button时将其设置回原始背景图像。Button

这是我当前的代码:

property bool bImgChange: true

Button {
    id: bu
    x: 121
    y: 40
    iconSource: "q.png"
    activeFocusOnPress: false
    opacity: 1
    checkable: true
    checked: true
    Layout.minimumWidth: 100
    Layout.minimumHeight: 100

    onClicked: {
        if ( bImgChange == true )
        {
            bu.iconSource = "index.png"
            bImgChange = false
        }
        else
        {
            bu.iconSource = "q.png"
            bImgChange = true
        }
    }
}

上面的代码仅在单击时设置背景图像,Button而我需要在按下和释放时更改背景图像。

4

1 回答 1

4

Button有一个pressed可以像这样使用的属性:

Item {
    Image { source: btn.pressed? "image1.png" : "image2.png"; ...}
    Button { id: btn; ... }
}

或者,如果你想改变 的iconSourceButton你可以尝试这样的事情:

Button {
    iconSource: pressed? "image1.png" : "image2.png"
}

希望能帮助到你。

于 2015-05-07T05:10:23.680 回答