在QtQuick ListView中从模型中获取标题标题

时间:2018-04-04 23:38:52

标签: qt listview qtquick2

我不清楚如何从ListView中的标题组件访问模型。我在(C ++)模型中提供了headerData,但它似乎没有被调用。我可以从RowDelegate中的模型中获取数据,因此我的角色定义正确(我已经实现了roleNames函数)。

我确定必须在某处的文档中解释这一点,但我无法找到它。

ListView{
    anchors.fill: parent   
    model: logfile_model
    orientation: Qt.Vertical

    header: Row{
        Text{
            text: increment  // Doesn't work
            width: 100
        }
        Text{
            text: iteration  // Doesn't work
            width: 100
        }
    }

    delegate: Row{                
        Text {
            text: increment
            width: 100
        }    
        Text {
            text: iteration
            width: 100
        }
    }
}

1 个答案:

答案 0 :(得分:1)

这是您想要的演示,但不推荐,因为标题文本不会放入模型数据中。

您可以拖动列表以查看固定的标题始终保持可见。

ListModel{
    id: testData
    ListElement{
        increment: "I am a Header"
        iteration: "I am also aHeader"
    }
    ListElement{
        increment: "testData"
        iteration: "testData"
    }
    ListElement{
        increment: "testData"
        iteration: "testData"
    }
    ListElement{
        increment: "testData"
        iteration: "testData"
    }
}

ListView{
    anchors.fill: parent
    model: testData

    Rectangle{ // always visible
        width: header.width
        height: header.height
        color: "white"
        Row{
            id: header
            Text{
                text: testData.get(0).increment
                width: 100
            }
            Text{
                text: testData.get(0).iteration
                width: 100
            }
        }
    }

    delegate: Row{
        Text {
            text: increment
            width: 100
        }
        Text {
            text: iteration
            width: 100
        }
        Component.onCompleted: if(index == 0) {
                                   visible = false
                               }
    }
}