简体   繁体   中英

QML Change Gradient when Button is Pressed

I need my button to changed gradient when pressed. I've tried the following code (suggested by official documentation):

Button {
  background: Rectangle {
    gradient: Gradient {
      GradientStop { position: 0 ; color: control.pressed ? "#ccc" : "#eee" }
      GradientStop { position: 1 ; color: control.pressed ? "#aaa" : "#ccc" }
    }
  }
}

However, my button turns out black. By substituing the word 'control' with the word 'this' or 'parent', I get the same result: the button is correctly coloured in case it is not pressed, but when I press it, nothing changes.

You forgot to give your button an id:control .

this.pressed and parent.pressed will be undefined , which means they will resolve to false .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM