Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to change models using TableView column click event

    QML and Qt Quick
    qml tableview onclick listmodel
    1
    2
    871
    Loading More Posts
    • 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
      Flesh last edited by Flesh

      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 Reply Quote 0
      • F
        Flesh @Flesh last edited by

        @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 Reply Quote 0
        • First post
          Last post