这篇帮助很大:
https://blog.csdn.net/qq_32116695/article/details/81298585, 代码如下:
TreeView
{
id: viewTree
anchors.fill: parent
backgroundVisible: false
model: treeViewModel;
sortIndicatorVisible:true
style:TreeViewStyle{
backgroundColor: "#2B2F33"
textColor: "#666666" //这个是设置分层的时候那个小三角的颜色
itemDelegate:Rectangle{
color: "transparent" //背景设置透明,不然在选中行的时候会出现选中颜色就一半的情况
RowLayout{ //这边的话就看个人的具体需求了,我这边是需要实现 图片+文字
Image {
id: name
source: if(styleData.value != "默认")
{
if(styleData.selected)
"./Image/分屏视图/" + styleData.row +"_press.png"
else if(styleData.value == "1-画面")
"./Image/分屏视图/" +"1.png"
else if(styleData.value == "4-画面")
"./Image/分屏视图/" +"2.png"
else if(styleData.value == "9-画面")
"./Image/分屏视图/" +"3.png"
else if(styleData.value == "16-画面")
"./Image/分屏视图/" +"4.png"
}
else {""}
}
Text {
color: "#FEFEFE"
elide: styleData.elideMode
text: styleData.value
padding: 10
onTextChanged: {
root.currentItemIndex(styleData.index) //发出信号,这个信号自己定义
}
}
}
}
rowDelegate:Rectangle{
id: rowDel
color: styleData.selected ? "#595F69" : root.color;
height: 28
}
}
TableViewColumn { //添加一列
title: "Default"
role: "text" //这个role的名字要跟你在model中定义的列的名字一模一样,
width: root.width
resizable: false
}
headerVisible:false
horizontalScrollBarPolicy: Qt.ScrollBarAlwaysOff
onClicked: {
root.currentChildIndex(viewTree.currentIndex)
}
onDoubleClicked: {
root.currentChildIndex(viewTree.currentIndex)
}
}