4

我有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 { ... }
    }
}
4

1 回答 1

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 { ... }
    }
}
于 2012-07-12T23:41:16.890 回答