因此,在测试我的程序时,我发现了最奇怪的事情。QML ListView在选择相邻元素时灰显或删除元素文本
所以,我有一个ListView元素与自定义C++模型,和一个相当简单的代表。每个委托都是一个MyButton类,它只是一个Text类(z:2),一个Image类(z:1)和一个MouseArea类。该Image类是背景,并包含半透明图像,当MouseArea onPressed()时它变得不透明。
现在奇怪的部分。
当ListView有4个元素时,它正常工作 - 除非用户选择条目#3,则选择条目#2或#1。
- 当所选条目从#3→#1进入时,条目#2中的文本变灰,而不是其正常的白色。
- 当所选条目从#3→#2时,条目#2中的文本完全消失。
经过测试的时间和砰的一声撞在桌子的头,我已经发现了多一点:
- myButton的或任何其子女的不透明度永远不会改变。
- myButton的文本元素的颜色不会改变
- 为myButton的测试元件的内容不会改变
- 部分抵消文本为myButton之外后,这种不正常的行为只会影响其余为myButton的形象小孩的范围内的文本。
- MyButton或其任何子级的Z级别都不会改变,尽管它看起来好像MyButton的图像被放置在其文本上。
- 另一个图像永远不会放在MyButton元素的顶部。如果是这种情况,从#3 - >#1进入时,您会看到#2条目的图像变暗。
- 当ListView滚动时,一切都恢复正常。
当ListView中包含4个元素,下面是异常:
- 当#4 - >#1:#2和#3灰色出
- 当#4 - >#2:# 2消失
- 当#4 - >#3:#3消失
- 当#3 - >#2:#2消失
- 当#3 - >#1:#2变灰
这与重新排序MyButton类中的图像和文本一致,将图像放置在文本上方Z级别。但是,在MyButton定义中强制使用z级别,并且在发生这些事件时决不会创建onZChanged信号。
下面是相关代码:
//MyButton:
import QtQuick 2.0
Item {
id: button
property string source: ""
property string source_toggled: source
property string button_text_alias: ""
signal pressed
width: button_image.sourceSize.width
height: button_image.sourceSize.height
property bool toggled: false
Image{
id: button_image
z: 1
source: toggled ? parent.source_toggled : parent.source
}
MyText{
z: 2
text_alias: button_text_alias
anchors.centerIn: parent
}
MouseArea {
id: button_mouse
anchors.fill: parent
onPressed: button.pressed()
}
}
//ListView:
Component{
id: p_button
MyButton{
source: picture_path + "bar.png"
source_toggled: picture_path + "bar_selected.png"
toggled: model.isCurrent
onClicked: {
profile_model.setCurrent(model.index)
}
button_text_alias: model.display
}
}
ListView{
id: p_list
width: 623
height: count*74 -1
spacing: 1
interactive: false
model: p_model
delegate: p_button
}
我想不出-anything-可能导致此行为..任何想法?