Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom style for all Qt Quick Button Control states

Tags:

c++

qt

qml

I'm looking for a way to create custom styles for all states of a button with QML. So far I have only found the following way to create a style that can be applied to multiple buttons

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
}

But this only changes the normal state of the button, and can not find a way to give a style when the botton is pressed.

like image 767
José María Avatar asked Dec 04 '25 17:12

José María


2 Answers

In ButtonStyle, you can use control.hovered and control.pressed to determine, when button is being hovered or pressed.

  ButtonStyle {
     background: Rectangle {
        width: control.width
        height: control.height
        color: control.pressed ? "gray" : "black"
     }
  }
like image 102
Meefte Avatar answered Dec 06 '25 08:12

Meefte


You can take a look at the example project named QtQuick Control Touch Gallery or you can see here for online version http://qt-project.org/doc/qt-5/qtquickcontrols-touch-content-buttonpage-qml.html

like image 38
fpermana Avatar answered Dec 06 '25 06:12

fpermana