Qt 如何通过索引QML为网格布局上的1项设置颜色?
我无法通过QML上的索引为网格布局上的项设置颜色。 我有一个矩形网格Qt 如何通过索引QML为网格布局上的1项设置颜色?,qt,colors,grid,qml,Qt,Colors,Grid,Qml,我无法通过QML上的索引为网格布局上的项设置颜色。 我有一个矩形网格 Grid { visible: true id: grid columns: matrixSize anchors.centerIn: parent Repeater{ model: 256 //model: serial.modelData Rectangle{ id: cell width
Grid {
visible: true
id: grid
columns: matrixSize
anchors.centerIn: parent
Repeater{
model: 256
//model: serial.modelData
Rectangle{
id: cell
width: square_size
height: square_size
border.color: "black"
border.width: 1
Text {
id: txt
anchors.centerIn: parent
// text: model.modelData
text: index
color: "black"
}
color:{
for(temp = 4; temp <14; temp++){
cell.color[temp] = "yellow"
}
}
}
}
}
网格{
可见:正确
id:网格
列:矩阵大小
anchors.centerIn:父对象
中继器{
型号:256
//型号:serial.modelData
长方形{
id:单元格
宽度:方形尺寸
高度:平方米
border.color:“黑色”
边框宽度:1
正文{
id:txt
anchors.centerIn:父对象
//文本:model.modelData
文本:索引
颜色:“黑色”
}
颜色:{
对于(temp=4;temp将代码的第22行更改如下:
color: (index >= 4 && index <= 14) ? "yellow" : "white";
color:(index>=4&&index您也可以尝试使用定位器。index查找中继器中项目的索引。检查文档使用:
Reapeater.itemAt(temp) = "yellow"
请澄清你的问题。你想在这里做什么?Rectangle.color
是color
类型的属性,你可以获取或设置它,而不是添加一些奇怪的逻辑。如果你想访问转发器的项目,你可以使用我想为某个矩形设置颜色(转发器中的矩形4到矩形14)