0

在下面的代码中,如果委托 Item 是 Rectangle,即使 Rectangle 继承了 Item,突出显示也不起作用,这是一个错误,还是我遗漏了什么。

ListView {
    width: 200
    height: 500
    focus: true
    highlight: Component {
        Rectangle {
            width: 200
            height: 20
            color: "red"
        }
    }

    model: ListModel {ListElement {name: "dummy 1"} ListElement {name: "dummy 2"}}
    delegate: Item { // If Item is replaced with Rectangle highlight does not work even though Rectangle inherits Item
        width: 200
        height: 20
        Text {
            text: name
        }
    }
}
4

1 回答 1

2

您必须将委托矩形的颜色设置为透明。默认情况下,矩形的颜色是白色,Item 是透明的。

ListView {
    width: 200
    height: 500
    focus: true
    highlight: Component {
        Rectangle {
           width: 200
           height: 20
           color: "red"
        }
    }

    model: ListModel {ListElement {name: "dummy 1"} ListElement {name: "dummy 2"}}
    delegate: Rectangle {
        width: 200
        height: 20
        color: "transparent"
        Text {
            text: name
        }
    }
}
于 2012-10-12T16:19:32.050 回答