所有 Qt Quick Button Control 状态的自定义样式

mis*_*iso 3 c++ qt qml

我正在寻找一种使用 QML 为按钮的所有状态创建自定义样式的方法。到目前为止,我只找到了以下方法来创建可以应用于多个按钮的样式

Component {
  id: leftButtonStyle

  ButtonStyle {

      background: Rectangle {
          width: control.width
          height: control.height
          color: "black"
      }

      label: Text {
          font.family: "Helvetica"
          font.pointSize: 10
          color: "#949599"
          text: control.text
      }
  }

}

Button {
    text:"Button One"
    style: leftButtonStyle
}

Button {
        text:"Button Two"
        style: leftButtonStyle
}
Run Code Online (Sandbox Code Playgroud)

但这只是改变按钮的正常状态,无法找到按下按钮时赋予样式的方法。

Mee*_*fte 5

在 中ButtonStyle,您可以使用control.hoveredcontrol.pressed来确定按钮何时被悬停或按下。

  ButtonStyle {
     background: Rectangle {
        width: control.width
        height: control.height
        color: control.pressed ? "gray" : "black"
     }
  }
Run Code Online (Sandbox Code Playgroud)