QML矩形不透明度仅为颜色

Mat*_*ias 1 opacity qml

我想在我的Rectangle上设置唯一的不透明度.但文本也变得透明.我怎样才能为背景设置.

Rectangle {
                id: button
                color: "black"
                opacity: 0.3
                width: parent.width
                height: 35

                    Text {
                        anchors.centerIn: parent
                        text: qsTr("text")
                        color: "white"
                        font.pixelSize: 25
                        }
            }
Run Code Online (Sandbox Code Playgroud)

Mit*_*tch 8

这在不透明度文档中有解释:

设置此属性后,指定的不透明度也将单独应用于子项.在某些情况下,这可能会产生意想不到的效果.例如,在下面的第二组矩形中,红色矩形指定了0.5的不透明度,即使子项未指定不透明度,也会影响其蓝色子矩形的不透明度.

您可以将Text项目移出:

Rectangle {
    id: button
    color: "black"
    opacity: 0.3
    width: parent.width
    height: 35
}

Text {
    anchors.centerIn: button
    text: qsTr("text")
    color: "white"
    font.pixelSize: 25
}
Run Code Online (Sandbox Code Playgroud)

或者给出Rectangle透明色而不是改变不透明度:

Rectangle {
    id: button
    color: "#33000000" // form: #AARRGGBB
    width: parent.width
    height: 35

    Text {
        anchors.centerIn: parent
        text: qsTr("text")
        color: "white"
        font.pixelSize: 25
    }
}
Run Code Online (Sandbox Code Playgroud)