qt - QML 中有没有办法更改多个事件的按钮颜色?
问题描述
我是 Qml 的新手,我有点迷茫。我想为图像或按钮的颜色设置多个状态。一个更简单的部分是,如果按下按钮,它应该改变颜色或图像。这可以通过以下方式实现:
Button {
width: 50
height: 50
background: Rectangle {
color: parent.pressed ? "red" : "blue"
}
}
在 Addtion 中,我还有状态:单击、启用、启用单击等等。
所以首先:我可以在没有 else 语句的情况下捕获 parent.pressed,例如:
if (parent.pressed)
color = "red"
elseif (onClicked)
color = "blue"
....
当我的后端禁用或启用我的任何 GUI 元素时,可能会设置其他状态,我可以要求并更改我的颜色。
所以基本上我有两个问题:如何在 if 语句中捕获事件以及如何捕获多个事件而不使用 if else 语句,例如:
parent.pressed ? "red" : "blue"
我更进一步,可以添加状态。
Button {
width: 50
height: 50
id: btn
onClicked: state="clicked"
onPressed: state="pressed"
background: Rectangle
{
anchors.fill: parent
}
states: [
State {
name: "clicked"
PropertyChanges { target: btn; background: color= "red" }
},
State {
name: "pressed"
PropertyChanges { target: btn; background: color= "blue" }
}
]
}
但这不会改变我的按钮的颜色,而是 UI 的其余部分。
解决方案
我猜你的代码中有语法错误。实际上它应该类似于以下内容:
Button {
id: btn
text: "Click me"
property color bgColor
anchors.centerIn: parent
state: pressed ? "pressed" : "regular"
background: Rectangle {
color: btn.bgColor
}
states: [
State {
name: "regular"
PropertyChanges {
target: btn
bgColor: "orange"
}
},
State {
name: "pressed"
PropertyChanges {
target: btn
bgColor: "green"
}
}
]
}
或者你可以玩Rectangle
:
background: Rectangle {
id: bg
}
states: [
State {
name: "regular"
PropertyChanges {
target: bg
color: "orange"
}
},
State {
name: "pressed"
PropertyChanges {
target: bg
color: "green"
}
}
]
推荐阅读
- javascript - 锁定正文滚动,但正文中的某些 div 可以滚动
- java - 两个几乎相同的方面导致奇怪的问题
- c# - 无法使用 C# 和管理员用户设置密码和启用帐户
- django - Django - 从模型中获取特定数据
- python - Python urllib3:一段时间后关闭空闲连接
- python - 如何针对 list_b 中的每个元素检查 list_a 的一个索引,并对 list_a 中的所有元素重复此过程?
- ruby-on-rails - 为什么 as_json 在 has_one 关联中不返回空对象?
- r - 如何使用 lmeresampler 从嵌套随机效应模型中获取系数的 p 值
- javascript - 当文件本身在服务器上呈现时如何在文件中获取浏览器的本地日期和时间
- excel - 根据 IF 命令的数字输入读取单元格