QML 如何在 mouseArea 中设置颜色

QML how to set color in a mouseArea

我想设计如下按钮布局:

它有一个位于蓝色背景之上的重启按钮图像。我想使用 QML 在 Qt 中复制相同的内容。我正在使用 MouseArea Qt Quick 对象,我在 Stretch 填充模式下重叠图像。但是,没有为鼠标区域设置蓝色背景的选项。目前看起来像这样:

相同的对应代码:

MouseArea {
    id: restartbutton
    x: 669
    width: 50
    height: 50
    opacity: 1
    antialiasing: false
    hoverEnabled: true
    anchors.top: parent.top
    anchors.topMargin: 8
    z: 1

    Image {
        id: image2
        anchors.fill: parent
        source: "restart_icon.png"
    }

}

这里如何设置MouseArea的背景?

您可能想像这样使用 Rectangle

Rectangle {
  width:50
  height: 50
  Image {
    anchors.fill:parent
    source: "restart_icon.png"
  }
  MouseArea {
    anchors.fill:parent
    onClicked: {...}
  }
}

查看 QML Advanced Tutorial 了解更多信息

我认为这样更容易阅读:

   MouseArea { id: clickArea
        anchors {
            fill: parent 
            margins: -10   // optional to enlarge the backgound / click zone
        }
        onClicked: {
            console.log("clicked!")
        }
        Rectangle {  // background, also allowing the click
            anchors.fill: clickArea
            border.color: "black"
            color: "lightblue"
            opacity: 0.3
        }
   }