import QtQuick 2.0 Item { width: 400 height: imgSwitchButton.height property string imgPath: "/qmlimages/off.png" property bool isSelected: false property alias text: mSwitchName.text signal selected Row{ anchors.fill: parent Text { id: mSwitchName font.pixelSize: 20 color: "white" } Image { anchors.right: parent.right width: 50 height: 30 id: imgSwitchButton source: imgPath MouseArea { anchors.fill: parent onClicked: { changeSwitchStatus() selected(isSelected) } } } } //改变switch按钮的状态 function changeSwitchStatus(){ console.log("changeSwitchStatus") if (!isSelected){ imgPath = "/qmlimages/on.png" }else{ imgPath = "/qmlimages/off.png" } isSelected = !isSelected } }