How to change models using TableView column click event



  • I have 2 static ListModel's in this example, in reality I use LocalStorage to fill the ListModel, but to keep it simple, I added to buttons to change the Models, but I want to tie it to the TableView's Header Column click event, and can not figure out how to do that from other examples of trying to sort, I do not know if it is possible to have a sort using ListModel, I could not find any example, so can someone explain this or show an example, of how to replace the buttons with column click events, I can then use this to pass the sort by argument to my LocalStorage sql statement to update the ListModel.

    import QtQuick 2.11
    import QtQuick.Controls 1.3
    import QtQuick.Layouts 1.3
    
    import QtQuick.Window 2.11
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("TableView Sort")
    
        Column {
            id: column
            spacing: 9
            anchors.fill: parent
    
            TableView {
                id: tableView
                anchors.left: parent.left
                anchors.leftMargin: 5
                anchors.right: parent.right
                anchors.rightMargin: 314
                model: myListModel1
                Layout.fillHeight: true
                Layout.fillWidth: true
                sortIndicatorVisible: true
    
                TableViewColumn {
                    role: "title"
                    title: "Column 1"
                    width: 133
                }
                TableViewColumn {
                    role: "description"
                    title: "Column 2"
                    width: 166
                }
            }
    
            Button {
                id: button1
                text: qsTr("Model 1")
                onClicked: {
                    tableView.model = myListModel1
                }
            }
    
            Button {
                id: button2
                text: qsTr("Model 2")
                onClicked: {
                    tableView.model = myListModel2
                }
            }
        }
    
        ListModel {
            id: myListModel1
    
            ListElement {
                title: "Orange"
                description: "Orange is Orange"
            }
            ListElement {
                title: "Banana"
                description: "Yellow"
            }
            ListElement {
                title: "Apple"
                description: "Red"
            }
        }
        ListModel {
            id: myListModel2
    
            ListElement {
                title: "Apple"
                description: "Red"
            }
            ListElement {
                title: "Banana"
                description: "Yellow"
            }
            ListElement {
                title: "Orange"
                description: "Orange is Orange"
            }
        }
    
    }
    
    

    Update: Adding this to the TableView Fixed is, see credit in below reply.

    onSortIndicatorColumnChanged: tableView.model = (sortIndicatorColumn == 0) ? myListModel1 : myListModel2
    onSortIndicatorOrderChanged: tableView.model = (sortIndicatorColumn == 0) ? myListModel1 : myListModel2
    

    Thanks



  • @Flesh Romha Korev on stackoverflow.com gave me this answer:

    onSortIndicatorColumnChanged: tableView.model = (sortIndicatorColumn == 0) ? myListModel1 : myListModel2
    onSortIndicatorOrderChanged: tableView.model = (sortIndicatorColumn == 0) ? myListModel1 : myListModel2
    
    


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