Selection in TableView
-
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 codeTableView { 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? -
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 } } } }
-
Documentation says:
Note: the
selected
andcurrent
properties must be defined as required. This will informTableView
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 itcurrent
.After further investigation using
QItemSelectionModel
fromQTableView
(shared model between QML and C++) these are my observations:- When item is clicked in QML,
current
is set, no selection is done - When item is clicked in
QTableView
(C++ Widgets) row isselected
and item is setcurrent
. This is then synchronised with QMLTableView
where the same item is marked ascurrent
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); } }
- When item is clicked in QML,