2017-06-14 41 views
1

我想设计下面的按键布局:QML如何设置颜色的鼠标区域

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

} 

如何设置后台鼠标区域吗?

回答

1

您可能需要使用Rectangle这样的:

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

看看在QML Advanced Tutorial进一步信息,请

+0

嗨!为了改善这个问题,你可以在你的例子中设置背景颜色。 – derM

+0

也可以将'MouseArea'作为根节点(使用'MouseArea'交换'Rectangle'),然后将'Rectangle's'color'暴露为'property alias background:myRect.color'。原因是,'MouseArea'比'Rectangle'具有更多可能与揭露有关的信号和属性。如果你想拥有一个干净的界面,并且只暴露一个最小值,那么作为根的'Item'就是另一个选择。 – derM

+0

是的,这是有效的。就像@derM所说的,最好有MouseArea作为根。 –