How can i enable selection on TableView in QML Qt6.3?
-
I'm having trouble in enabling selection on QML model/view in Qt6.3.
In order to understand the behaviour i took the example given by Qt on the documentation of Table View about selection (see here).
This is my whole main.qml file:import QtQuick import QtQuick.Controls import Qt.labs.qmlmodels Window { id: mainWin visible: true width: 640 height: 480 title: qsTr("QML Test") 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 } } } }
and a very basic main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { #if defined(Q_OS_WIN) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
The application start, and the table is shown with proper data, but rows are not selectable.
What am i missing?Thank you.
Michele -
@belox86 said in How can i enable selection on TableView in QML Qt6.3?:
What am i missing?
The fact that
TableView
doesn't provide the UI to select rows automatically.If you want to be able to select cells by long click or drag, you can use a
SelectionRectangle
If you want to have checkboxes to select rows, you can add this in your tableView:
leftMargin: checkColumn.width VerticalHeaderView { id: checkColumn parent: tableView syncView: tableView selectionModel: tableView.selectionModel boundsBehavior: tableView.boundsBehavior delegate: CheckBox { required property int row required property bool selected checked: selected onToggled: tableView.selectionModel.select(tableView.model.index(row, 0), ItemSelectionModel.Toggle | ItemSelectionModel.Rows) } }
-
ok, thank you very much.
that's no where mentioned in TableView documentation.So i went back with my original code, which actually was a TreeView in which i want to highlight the selected row.
The model is a c++ implemented QAbstractItemModel which is set to the qml context from the main.cppimport QtQuick import QtQuick.Controls Window { id: mainWin visible: true width: 640 height: 480 title: qsTr("QML Test") TreeView { id: treeView anchors.fill: parent model: projectModel selectionModel: ItemSelectionModel { model: treeView.model } delegate: TreeViewDelegate { id: treeDelegate required property bool selected required property int row background: Rectangle { opacity: treeDelegate.selected ? 1 : 0 color: "red" } onClicked: { treeView.selectionModel.select(treeView.modelIndex(treeDelegate.row, 0), ItemSelectionModel.Toggle | ItemSelectionModel.Rows) console.log("clicked:", treeView.model.data(treeView.modelIndex(treeDelegate.row, 0))) console.log("Selection model selected? ", treeView.selectionModel.isSelected(treeView.modelIndex(treeDelegate.row, 0))) console.log("Delegate selected? ", treeDelegate.selected) } } }
The selection works perfecly on 0-depth items for which the row become red.
the log on first click sayqml: clicked: Parent1 qml: Selection model selected? true qml: Delegate selected? true
and on the second (toggle):
qml: clicked: Parent1 qml: Selection model selected? false qml: Delegate selected? false
So far so good.
Unfortunatly if i click on 1-depth item the row does not color and i get this log:qml: clicked: Child1 qml: Selection model selected? true qml: Delegate selected? false
So the clicked item is properly indexed (i retrive the proper dysplayRole with the used index), the selection model actually select it BUT the delegate does not update the selected properties...
any idea on what's wrong? -
sry, i will open a dedicate topic and mark this as resolved.
Thank you