【问题标题】:Custom style for all Qt Quick Button Control states所有 Qt Quick Button Control 状态的自定义样式
【发布时间】:2014-11-12 22:26:41
【问题描述】:

我正在寻找一种使用 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
}

但这只是改变了按钮的正常状态,并没有办法在按钮被按下时赋予样式。

【问题讨论】:

  • 实现问题属于 StackOverflow。
  • @GlenH7 那么,我该如何移动它呢?我想我没有这方面的特权。
  • 您可以标记问题并请求版主迁移。我也投票支持迁移,但目前只有一票。

标签: c++ qt qml


【解决方案1】:

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

  ButtonStyle {
     background: Rectangle {
        width: control.width
        height: control.height
        color: control.pressed ? "gray" : "black"
     }
  }

【讨论】:

  • 非常感谢。现在我开始了解它是如何工作的。
【解决方案2】:

您可以查看名为QtQuick Control Touch Gallery的示例项目,也可以在此处查看在线版本http://qt-project.org/doc/qt-5/qtquickcontrols-touch-content-buttonpage-qml.html

【讨论】: