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. Selection in TableView
Forum Updated to NodeBB v4.3 + New Features

Selection in TableView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 1.2k 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.
  • S Offline
    S Offline
    Svan96
    wrote on last edited by
    #1

    Hi,
    I am using Qt 6.6 and I am trying to use the selection mode of TableView with the DelegateChooser component.
    I mainly adhered to the official documentation of the TableView component: https://doc.qt.io/qt-6/qml-qtquick-tableview.html#selecting-items
    Here is a simplified version of my code

    TableView {
                id: tableView
                selectionMode: TableView.SingleSelection
                selectionBehavior: TableView.SelectRows
                animate: false
                clip: true
    
                model: SelectionTableModel {
                    // a table model implemented in C++
                }
    
                selectionModel: ItemSelectionModel {}
                delegate: DelegateChooser {
                    role: "type"
                    DelegateChoice {
                        roleValue: SelectionTableModel.String
                        delegate: Rectangle {
                            required property bool selected
                            required property bool current
                            border.width: 1
                            implicitHeight: 36
                            color: selected ? "grey" : "white"
    
                            Text {
                                text: model.value
                                anchors.verticalCenter: parent.verticalCenter
                                padding: 5
                                font.preferShaping: false
                            }
                        }
                    }
    
                    DelegateChoice {
                        roleValue: SelectionTableModel.Boolean
                        delegate: CheckBox {
                            required property bool selected
                            required property bool current
                            checked: model.value
                            checkable: false
                        }
                    }
                }
            }
    

    According to the documentation, the TableView component should take responsibility to update the selected and current property. However, these properties are always false and do not change when I am clicking anywhere on the table.
    Am I missing something from the documentation?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KejPi
      wrote on last edited by
      #2

      Did you manage to solve the issue? I am trying to implement selection in TableView and it seems only current is working.
      I have basically taken the code from documentation and put it in a simple example application and selection does not work.

      In this code, selected item shall be blue, current has border. When I click on any item in the table, it is made current (border appears) but never selected.

      import QtQuick
      import Qt.labs.qmlmodels
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
      
      
          TableView {
              id: tableView
              anchors.fill: parent
              clip: true
      
              model: TableModel {
                  TableModelColumn { display: "name" }
                  rows: [ { "name": "Harry" }, { "name": "Hedwig" }, { "name": "XXXX" } ]
              }
      
              selectionModel: ItemSelectionModel {
                  model: tableView.model
              }
      
              delegate: Rectangle {
                  implicitWidth: 100
                  implicitHeight: 30
                  color: selected ? "blue" : "lightgray"
                  border.width: current ? 1 : 0
      
                  required property bool selected
                  required property bool current
      
                  Text { text: display }
              }
          }
      }
      
      JoeCFDJ 1 Reply Last reply
      0
      • K KejPi

        Did you manage to solve the issue? I am trying to implement selection in TableView and it seems only current is working.
        I have basically taken the code from documentation and put it in a simple example application and selection does not work.

        In this code, selected item shall be blue, current has border. When I click on any item in the table, it is made current (border appears) but never selected.

        import QtQuick
        import Qt.labs.qmlmodels
        
        Window {
            width: 640
            height: 480
            visible: true
            title: qsTr("Hello World")
        
        
            TableView {
                id: tableView
                anchors.fill: parent
                clip: true
        
                model: TableModel {
                    TableModelColumn { display: "name" }
                    rows: [ { "name": "Harry" }, { "name": "Hedwig" }, { "name": "XXXX" } ]
                }
        
                selectionModel: ItemSelectionModel {
                    model: tableView.model
                }
        
                delegate: Rectangle {
                    implicitWidth: 100
                    implicitHeight: 30
                    color: selected ? "blue" : "lightgray"
                    border.width: current ? 1 : 0
        
                    required property bool selected
                    required property bool current
        
                    Text { text: display }
                }
            }
        }
        
        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • K Offline
          K Offline
          KejPi
          wrote on last edited by KejPi
          #4

          Documentation says:

          Note: the selected and current properties must be defined as required. This will inform TableView that it should take responsibility for updating their values.

          So my understanding is that it takes care of setting item selected when user clicks on item like it does for setting it current.

          After further investigation using QItemSelectionModel from QTableView (shared model between QML and C++) these are my observations:

          1. When item is clicked in QML, current is set, no selection is done
          2. When item is clicked in QTableView (C++ Widgets) row is selected and item is set current. This is then synchronised with QML TableView where the same item is marked as current and the row is selected.

          But I want to achieve the same behavior as QTableView - by clicking on the item in QML TableView, that item shall be set as current and the row shall be selected. When selected row is removed from the model, selection is cleared and no row is selected.

          EDIT: It seems I have figured it out, maybe it helps someone. The delegate needs to handle selection using MouseArea:

          MouseArea {
              anchors.fill: parent
              onClicked: {
                  tableView.selectionModel.select(tableView.model.index(row, 0), ItemSelectionModel.ClearAndSelect | ItemSelectionModel.Current | ItemSelectionModel.Rows);
              }
          }
          
          1 Reply Last reply
          0

          • Login

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