使用`contentItem`来改变按钮的文字和背景颜色,可以通过绑定`contentItem`的属性来实现。`contentItem`是一个按钮的内部元素,可以访问和修改其属性。
示例代码:
```qml
import QtQuick 2.0
import QtQuick.Controls 2.0
ApplicationWindow {
visible: true
width: 400
height: 300
title: "Change Button Text and Background Color"
Button {
id: myButton
text: "Click me"
color: "blue"
onClicked: {
if (myButton.contentItem.text === "Click me") {
myButton.contentItem.text = "Button Clicked"
myButton.contentItem.color = "red"
} else {
myButton.contentItem.text = "Click me"
myButton.contentItem.color = "blue"
}
}
anchors.centerIn: parent
}
}
```
上述示例中,通过`myButton.contentItem`来访问按钮的`contentItem`,然后修改其`text`属性和`color`属性来改变按钮的文字和背景颜色。