Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt6.2 TableView SelectionModel / Rectangle selection not working



  • Hi there,

    I am trying to use a ItemSelectionModel in conjunction with a SelectionRectangle and can not get it to work.

    According to the Qt6 documentation (https://doc.qt.io/qt-6/qml-qtquick-controls2-selectionrectangle.html), this is a minimum example of how to get cell selection working, but nothing really seems to happen.

    I am on Qt 6.2, Ubuntu 20.04.

    Any hints?

    import QtQuick
    import QtQuick.Controls
    import Qt.labs.qmlmodels 1.0
    import QtQuick.Window
    
    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" } ]
           }
    
           selectionModel: ItemSelectionModel {
               model: tableView.model
           }
    
           delegate: Rectangle {
               implicitWidth: 100
               implicitHeight: 30
               color: selected ? "blue" : "lightgray"
    
               required property bool selected
    
               Text { text: display }
           }
       }
    
       SelectionRectangle {
           target: tableView
       }
    }
    


  • This behavior is by design.
    You need either to set tableView.interactive: false
    or

    SelectionRectangle { 
      selectionMode: SelectionRectangle.PressAndHold 
    }
    

    It's documented here.



  • This behavior is by design.
    You need either to set tableView.interactive: false
    or

    SelectionRectangle { 
      selectionMode: SelectionRectangle.PressAndHold 
    }
    

    It's documented here.



  • Thanks!

    Would be great if the example itself would also contain either interactive: false or the selectionMode.


Log in to reply