Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to change models using TableView column click event

How to change models using TableView column click event

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmltableviewonclicklistmodel
2 Posts 1 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Flesh
    wrote on 6 Apr 2019, 19:55 last edited by Flesh 4 Jul 2019, 18:06
    #1

    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

    Jeffrey Scott Flesher PhD
    http//LightWizzard.com/

    F 1 Reply Last reply 7 Apr 2019, 18:01
    0
    • F Flesh
      6 Apr 2019, 19:55

      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

      F Offline
      F Offline
      Flesh
      wrote on 7 Apr 2019, 18:01 last edited by
      #2

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

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

      Jeffrey Scott Flesher PhD
      http//LightWizzard.com/

      1 Reply Last reply
      0

      2/2

      7 Apr 2019, 18:01

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved