import QtQuick 2.15 import QtQuick.Controls 2.15 Button { id: root property alias text: btnText.text property string defaultColor: "transparent" property string hoverColor: "gold" property string redColor: "red" property string greenColor: "green" // 默认样式 background: Rectangle { color: root.defaultColor border.color: "transparent" border.width: 0 radius: 0 } contentItem: Text { id: btnText text: root.text font.family: "微软雅黑" font.pixelSize: 14 font.bold: true color: "white" horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter anchors.fill: parent } // 鼠标悬停时的颜色变化 MouseArea { anchors.fill: parent hoverEnabled: true onEntered: root.background.color = root.hoverColor onExited: root.background.color = root.defaultColor } // 切换颜色方法 function toDefault() { root.defaultColor = "transparent" root.hoverColor = "gold" root.background.color = root.defaultColor } function toRed() { root.defaultColor = root.redColor root.hoverColor = "white" root.background.color = root.defaultColor } function toGreen() { root.defaultColor = root.greenColor root.hoverColor = "white" root.background.color = root.defaultColor } }