下面是我的代碼設爲Qml:如何訪問按鈕樣式對象使用JavaScript一個按鈕裏面QML 5.2
Button {
id: newMenu
anchors {
top: topMenu.top
topMargin: 15
left: topMenu.left
leftMargin: 16
}
text: "New"
iconSource: "../images/New.png"
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: true //this line will enable mouseArea.containsMouse
onClicked: {
newProjectFileDlg.visible = true
}
onEntered: {
console.log(tt1);
}
}
style: ButtonStyle {
id: buttonStyle
background: Rectangle {
id: tt1
implicitWidth: 100
implicitHeight: 25
border.width: 0
radius: 4
color: mousearea.entered ? "lightsteelblue" : "#2e2e2e"
}
}
我要訪問此按鈕的樣式屬性,改變background.color當鼠標懸停是。但console.log輸出總是
qrc:/qmls/menu.qml:40: ReferenceError: tt1 is not defined
如何使用JavaScript獲取元素?或者我們有其他的方法來改變輸入鼠標的背景顏色。
你爲什麼要使用TT1,而不是按鈕樣式?這是否會引發類似的ReferenceError? – lpapp
@LaszloPapp是的,它輸出qrc:/qmls/menu.qml:40:ReferenceError:buttonStyle沒有定義 –
嗨。我不確定是否你正在尋找,但你可以簡單地使用「控制」來改變ButtonStyle的背景顏色,就像這樣: color:control.pressed? 「#f00」:(control.hovered || control.activeFocus?「#0f0」:「#00f」) – deste