Javascript 如何更改QML中TableView第一行的颜色?
我正在使用Javascript 如何更改QML中TableView第一行的颜色?,javascript,qt,qml,qt5,Javascript,Qt,Qml,Qt5,我正在使用QtQuick.Controls 1.4的TableView 这是行代理。我只想要第一排蓝色,其余的(空的或非空的)绿色 rowDelegate: Rectangle { border.width: 1 height: 50 width: 2000 color: {
QtQuick.Controls 1.4的TableView
这是行代理
。我只想要第一排蓝色,其余的(空的或非空的)绿色
rowDelegate:
Rectangle
{
border.width: 1
height: 50
width: 2000
color: {
var item = mymodel.get( styleData.row )
if (item.index1 === "1")
return "blue"
return "green"
}
}
现在,我的问题是,这个代码确实将第一行着色为蓝色,但它也将空行着色为蓝色
解决这个问题的方法是什么?
我不久前遇到了同样的情况。为此,我使用了headerDelegate
来进行同样的计算。以下是一个例子:
import QtQuick.Controls 1.0
import QtQuick.Controls.Styles 1.4
style:TableViewStyle {
rowDelegate: Rectangle{
id:rowRectangle
color:"green"
width: 2000
height: 40
}
headerDelegate: Rectangle{
height: 40
width: 2000
color: "blue"
}
}
请记住,这会将空行设置为绿色。您可以根据模型中的条目数调整表格高度。原因
文件说明:
注意:出于性能原因,创建的代理可以循环使用
跨多个表行。这意味着当你使用
隐式属性,如styleData.row或model,这些值可以
在构造委托后进行更改。这意味着你
当Component.onCompleted为时,不应假定内容是固定的
调用,但依赖于对此类属性的绑定。
注:重点是我的
解决方案
引文中强调的部分显示了解决方案,即创建到样式数据的绑定。行
:
rowDelegate: Rectangle {
color: (styleData.row === 0) ? "blue" : "green"
width: 2000
height: 40
border.width: 1
}
例子
下面是我为您编写的一个示例,用于演示建议的解决方案:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 1.4
Window {
width: 320
height: 200
visible: true
title: qsTr("TableView")
ListModel {
id: libraryModel
ListElement {
title: "A Masterpiece"
author: "Gabriel"
}
ListElement {
title: "Brilliance"
author: "Jens"
}
ListElement {
title: "Outstanding"
author: "Frederik"
}
}
TableView {
anchors.fill: parent
model: libraryModel
rowDelegate: Rectangle {
color: (styleData.row === 0) ? "blue" : "green"
width: 2000
height: 40
border.width: 1
}
TableViewColumn {
role: "title"
title: "Title"
width: 100
}
TableViewColumn {
role: "author"
title: "Author"
width: 200
}
}
}
结果
提供的示例产生以下结果:
这有什么帮助?“我希望这一行变成蓝色,而不是标题。@Aquarius_Girl看起来我在这种情况下理解错了问题。”。对不起,没关系!。不用担心。@scopchanov为什么要从标题中删除单词QML?这个问题已经标记为QML。没有必要重复标题中的内容。@scopchanov有必要。当问题出现在“类似问题”列表或谷歌搜索中时,标签在这些情况下不可见,人们的时间被浪费在知道他们不想或确实想为QML提供此解决方案上。从答案来看:,因此,标签只有当它们是标题的有机组成部分时才会在标题中占有一席之地:
。就我而言,这是问题的一部分。这是句子的一部分。我没有将它用作[QML]等。我将把问题还原为原来的形式@斯科普切诺夫