我的CoolButton
状态为pressed
:
// CoolButton.qml:
BorderImage {
...
states: State {
name: "pressed"
when: mouseArea.pressed == true
PropertyChanges { target: shade; opacity: 0.5 }
}
}
MenuButton
延伸CoolButton
:
// MenuButton.qml:
CoolButton {
...
states: State {
name: "pressed"
PropertyChanges { ... }
}
}
但是,pressed
中定义的MenuButton
状态似乎根本不起作用。它是否被pressed
中定义的CoolButton
状态隐藏了?我该如何覆盖呢?
应该是这样的吗?
// MenuButton.qml:
CoolButton {
...
states: State {
name: "pressed"
extend: "CoolButton.pressed"
PropertyChanges { ... }
}
}
答案 0 :(得分:2)
我是QML的新手,但据我所知,你无法扩展或覆盖已定义的组件。 相反,你可以封装它们,如下所示:
// CoolButton.qml:
BorderImage {
...
states: State {
name: "pressed"
when: mouseArea.pressed == true
PropertyChanges { target: shade; opacity: 0.5 }
}
}
和封装可能如下所示:
// MenuButton.qml:
CoolButton {
property alias cb: encapsulatedCB
CoolButton {id: encapsulatedCB; }
...
states: State {
name: "pressed"
PropertyChanges { ... }
}
}