Using TableView as ListView-delegate



  • I have a model that contains several QStandardItemModels. Now I want to create a view that displays a TableView for each of these QStandardItemModels.
    I had the idea to have a ListView that has a TableView as delegate, something like this:

    ListView {
        id: myListView
    
        anchors {
            fill: parent
            margins: 5
        }
    
        model: testModel
        delegate: CompareDelegate { }
    }
    

    And in CompareDelegate.qml:

    Item {
        id: base
    
        width: 500
        height: 300
    
        TableView {
            anchors.fill: parent
        }
    }
    

    How do I get the TableView inside the delegate to use the appropriate QStandardItemModel within the model of the ListView?
    I hope the question is somewhat clear.

    Thanks in advance.


  • Moderators

    @qwasder85 A simple way would be to keep a Q_INVOKABLE function or a public slot in your model which will return these individual QStandardItemModels based on some property. Now since the TableView is inside the delegate assigned to ListView you can make use of index property and pass it to the function which will return the model. Something like

    Item {
        id: base
        ...
        TableView {
            anchors.fill: parent
            model: myListView.model.getModel(index) //or testModel.getModel(index)
        }
    }
    


  • @p3c0 This sounds like a very easy solution, I'll try it out. Thank you.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.