I'm having an issue with a Qt Quick custom button. The planned behaviour is the button background, border, and text changes color on hover and pressed actions. The color change aspect is working, but when the button is pressed, it appears to lose it's radius setting (image of button states). I thought this might be related to the highlighted setting, but setting that to false, doesn't fix it. Any ideas what might be causing this behaviour?
import QtQuick 2.15
import QtQuick.Controls 2.15
Button {
id: customButton
text: qsTr("Custom Button")
highlighted: false
flat: true
implicitWidth: 200
implicitHeight: 40
// Custom Properties
// Button Colors
property color defaultButtonColor: "#DB8000"
property color hoverButtonColor: "#784491"
property color pressedButtonColor: "#315718"
// Text Colors
property color defaultTextColor: "#ffffff"
property color hoverTextColor: "#ffffff"
property color pressedTextColor: "#000000"
QtObject {
id: internal
property var dynamicColor: if (customButton.down) {
customButton.down ? pressedButtonColor : defaultButtonColor
} else {
customButton.hovered ? hoverButtonColor : defaultButtonColor
}
property var dynamicText: if (customButton.down) {
customButton.down ? pressedTextColor : defaultTextColor
} else {
customButton.hovered ? hoverTextColor : defaultTextColor
}
}
background: Rectangle {
id:bg_customButton
radius: 15
color: internal.dynamicColor
border.color: "#000000"
}
contentItem: Item {
id: item1_customButton
Text {
id: text_customButton
text: customButton.text
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
color: internal.dynamicText
}
}
}
Edit: Don't think it's the radius changing, something is being displayed over the baackground during the pressed event as the border is partial visible if more contrasting colors are used pressed button edges
Edit#2: Same behaviour when trying to use States, as suggested by @TOHO, but there could be errors in this given it's a first attempt as using them.
import QtQuick 2.15
import QtQuick.Controls 2.15
Button {
id: customButton
// Button Colors
property color defaultButtonColor: "#DB8000"
property color hoverButtonColor: "#784491"
property color pressedButtonColor: "#315718"
// Border Colors
property color defaultBorderColor: "#ffffff"
property color hoverBorderColor: "#ffffff"
property color pressedBorderColor: "#FF0800"
// Text Colors
property color defaultTextColor: "#ffffff"
property color hoverTextColor: "#ffffff"
property color pressedTextColor: "#000000"
text: qsTr("Custom Button")
clip: false
highlighted: false
flat: true
hoverEnabled: true
implicitWidth: 200
implicitHeight: 40
onHoveredChanged: if (hovered) {bg_customButton.state = "HOVER"} else {bg_customButton.state = "DEFAULT"}
onReleased: bg_customButton.state = "DEFAULT"
onPressed: bg_customButton.state = "PRESS"
background: Rectangle {
id: bg_customButton
radius: 15
border.color: defaultBorderColor
state: "DEFAULT"
states: [
State {
name: "DEFAULT"
PropertyChanges { target: bg_customButton; color: defaultButtonColor; border.color: defaultBorderColor}
PropertyChanges { target: text_customButton; color: defaultTextColor}
},
State {
name: "HOVER"
PropertyChanges { target: bg_customButton; color: hoverButtonColor; border.color: hoverBorderColor}
PropertyChanges { target: text_customButton; color: hoverTextColor}
},
State {
name: "PRESS"
PropertyChanges { target: bg_customButton; color: pressedButtonColor; border.color: pressedBorderColor}
PropertyChanges { target: text_customButton; color: pressedTextColor}
}
]
}
contentItem: Item {
id: item1_customButton
clip: true
Text {
id: text_customButton
text: customButton.text
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
state: "DEFAULT"
}
}
}