2017-06-14 45 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作爲根。 –