Qt QML:setProperty无效
当我尝试更改ListModel中包含的项的属性值时,以下代码无效: Main.qmlQt QML:setProperty无效,qt,qml,qt5,Qt,Qml,Qt5,当我尝试更改ListModel中包含的项的属性值时,以下代码无效: Main.qml import QtQuick 2.0 Item { anchors.fill: parent ListModel { id: modelCrayon } Component.onCompleted: { for (var i = 0; i < 10; i++) modelCrayon.append( { _tag: i, _source
import QtQuick 2.0
Item {
anchors.fill: parent
ListModel { id: modelCrayon }
Component.onCompleted: {
for (var i = 0; i < 10; i++)
modelCrayon.append( { _tag: i, _source: "resources/crayon-green.png", _selected: false } )
}
Column {
x: -170
spacing: 0
Repeater {
model: modelCrayon
delegate: Crayon {
tag: _tag
source: _source
selected: _selected
onCrayonSelected: {
for (var i = 0; i < modelCrayon.count; i++) {
if (i == tag) continue;
modelCrayon.setProperty(i, "_selected", false);
}
}
}
}
}
}
控制台上没有显示任何内容,因此“选定”变量永远不会更改。
我肯定我错过了一些明显的东西
顺便问一下,有没有更聪明的方法将ListModel用作选项框?我的意思是,我一次只需要一个项,并且必须具有selected属性==true。或者,换句话说,跟踪所选索引。这是实现我要求的工作代码。但它不能回答为什么没有设置属性
ListView {
id: list
anchors.verticalCenter: parent.verticalCenter
height: parent.height
x: -150
spacing: 0
orientation: ListView.Vertical
focus: true
model: modelCrayon
delegate: Crayon {
id: delegate
source: _source
selected: ListView.isCurrentItem
MouseArea {
anchors.fill: parent
onClicked: list.currentIndex = index
}
}
}
我已经测试了您的示例代码(专栏版本),它在Qt5.4/Windows7 64位上运行良好
你的跑步环境是什么?我不知道这是否是一个“真实”的答案。我想说这是一个解决办法,但我解决了在自定义Linux buildroot环境下更改ListView中的列并在RPi2上将所选属性设置为ListView.isCurrentItem.Qt 5.5的问题
ListView {
id: list
anchors.verticalCenter: parent.verticalCenter
height: parent.height
x: -150
spacing: 0
orientation: ListView.Vertical
focus: true
model: modelCrayon
delegate: Crayon {
id: delegate
source: _source
selected: ListView.isCurrentItem
MouseArea {
anchors.fill: parent
onClicked: list.currentIndex = index
}
}
}