QML如何在mouseArea中设置颜色

时间:2017-06-14 08:52:53

标签: qt qml qtquick2 mousearea

我想设计以下按钮布局:

Link

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

Link

相同的相应代码:

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的背景?

2 个答案:

答案 0 :(得分:1)

您可能希望像这样使用Rectangle

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

查看QML Advanced Tutorial了解更多信息

答案 1 :(得分:0)

我认为这样更容易阅读:

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