QT-Quick按钮半径问题
我对QT快速自定义按钮有问题。计划的行为是按钮背景,边框和文本在悬停和按压动作上更改颜色。颜色变化方面正在工作,但是按下按钮时,它似乎会丢失其半径设置(按钮状态)。我认为这可能与突出显示的设置有关,但是将其设置为false,并不能解决它。有什么想法可能导致这种行为?
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
}
}
}
编辑:不要认为这是半径的变化,在按压事件期间,在baackground上显示了一些东西,因为使用了更多对比颜色按钮边缘
编辑#2 :尝试使用@toho建议时的行为,但是考虑到这是使用它们的第一次尝试,可能会出现错误。
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"
}
}
}
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"
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是
Hoverenabled
是错误的(默认情况下),正如我在评论中所说的那样,这不是最好的练习,而是一个有效的例子:
your problem was that
hoverEnabled
was false (by default),and as I said in comments this is not best practice but here is a working example: