要在Qt QML中设置Button在鼠标滑过时的图片,可以使用MouseArea来处理鼠标事件,并在鼠标进入和离开时更改Button的source属性。
Button {
id: button
width: 100
height: 100
Image {
id: image
source: "normal.png" // 默认图片
}
MouseArea {
anchors.fill: parent
hoverEnabled: true
onEntered: {
image.source = "hover.png"; // 鼠标进入时的图片
}
onExited: {
image.source = "normal.png"; // 鼠标离开时的图片
}
}
}
在这个示例中,我们将一个Button放置在一个MouseArea中,并设置hoverEnabled属性为true,以启用鼠标滑过事件。当鼠标进入MouseArea时,我们将Image的source属性更改为"hover.png",当鼠标离开时,我们将source属性更改为"normal.png"。
您需要将"normal.png"和"hover.png"替换为您实际的图片路径。确保这些图片文件在项目中是可访问的。