Table selection not working
-
Hi there. I am having trouble getting QML TableView selection with an ItemSelectionModel even in a basic sense. I'm running this with a Python 3.12 base and looks like Qt 6.8.2. The python main.py has not been altered from the standard example though, this is all in QML.
The following code gives me this error when a cell is clicked:
TypeError: Passing incompatible arguments to C++ functions from JavaScript is not allowed.
import QtQuick import QtQuick.Window import Qt.labs.qmlmodels Window { width: 640 height: 480 visible: true title: qsTr("Hello World") TableView { id: tableView anchors.fill: parent selectionModel: ItemSelectionModel { model: tableView.model } model: TableModel { TableModelColumn { display: "checked" } TableModelColumn { display: "amount" } TableModelColumn { display: "fruitType" } TableModelColumn { display: "fruitName" } TableModelColumn { display: "fruitPrice" } // Each row is one type of fruit that can be ordered rows: [{ "checked"// Each property is one cell/column. : false, "amount": 1, "fruitType": "Apple", "fruitName": "Granny Smith", "fruitPrice": 1.50 }, { "checked": true, "amount": 4, "fruitType": "Orange", "fruitName": "Navel", "fruitPrice": 2.50 }, { "checked": false, "amount": 1, "fruitType": "Banana", "fruitName": "Cavendish", "fruitPrice": 3.50 }, { "checked": false, "amount": 1, "fruitType": "Banana", "fruitName": "Cavendish", "fruitPrice": 3.50 }, { "checked": false, "amount": 1, "fruitType": "Banana", "fruitName": "Cavendish", "fruitPrice": 3.50 }, { "checked": false, "amount": 1, "fruitType": "Banana", "fruitName": "Cavendish", "fruitPrice": 3.50 }, { "checked": false, "amount": 1, "fruitType": "Banana", "fruitName": "Cavendish", "fruitPrice": 3.50 }, { "checked": false, "amount": 1, "fruitType": "Banana", "fruitName": "Cavendish", "fruitPrice": 3.50 }, { "checked": false, "amount": 1, "fruitType": "Banana", "fruitName": "Cavendish", "fruitPrice": 3.50 }, { "checked": false, "amount": 1, "fruitType": "Banana", "fruitName": "Cavendish", "fruitPrice": 3.50 }, { "checked": false, "amount": 1, "fruitType": "Banana", "fruitName": "Cavendish", "fruitPrice": 3.50 }, { "checked": false, "amount": 1, "fruitType": "Banana", "fruitName": "Cavendish", "fruitPrice": 3.50 }, { "checked": false, "amount": 1, "fruitType": "Banana", "fruitName": "Cavendish", "fruitPrice": 3.50 }, { "checked": false, "amount": 1, "fruitType": "Banana", "fruitName": "Cavendish", "fruitPrice": 3.50 }, { "checked": false, "amount": 1, "fruitType": "Banana", "fruitName": "Cavendish", "fruitPrice": 3.50 }, { "checked": false, "amount": 1, "fruitType": "Banana", "fruitName": "Cavendish", "fruitPrice": 3.50 }, { "checked": false, "amount": 1, "fruitType": "Banana", "fruitName": "Cavendish", "fruitPrice": 3.50 }, { "checked": false, "amount": 1, "fruitType": "Banana", "fruitName": "Cavendish", "fruitPrice": 3.50 }] } delegate: Rectangle { required property bool selected Rectangle { anchors.fill: parent color: "#3300FF00" visible: selected } MouseArea { anchors.fill: parent onClicked: { tableView.selectionModel.select( tableView.model.index, ItemSelectionModel.Rows) } } implicitWidth: 100 implicitHeight: 20 border.color: "black" Text { id: t anchors.centerIn: parent text: display } } } }
Note that I also tried to use ItemSelectionModel.Select in the select call but that gives the same error.
I Googled and queried AI every way I could think of but most results looked like what I have here. Also this seems to match the documentation for TableView itself. Can anyone see why this is happening? Thank you.
Update: The 'tableView.model.index' reference didn't look right to me inside the delegate so I changed it to just 'index' but alas, the same error.
-
i think due to this
tableView.selectionModel.select(
tableView.model.index,
ItemSelectionModel.Rows)you were passing ItemSelectionModel.Rows which is obviously a QtQuick object not a javascript object, while "select" is a method created in javascript, im not sure though
-
i think due to this
tableView.selectionModel.select(
tableView.model.index,
ItemSelectionModel.Rows)you were passing ItemSelectionModel.Rows which is obviously a QtQuick object not a javascript object, while "select" is a method created in javascript, im not sure though
@jhayar Thank you, that makes sense. But ItemSelectionModel.Select doesn't work either. So I'm not sure what you are supposed to use there.
Update: I figured out what the problem was. The standard practice is to use a bitwise or with SelectionModel objects. Also I got the index line wrong from the start. The below works:
tableView.selectionModel.select( tableView.model.index(row, 0), ItemSelectionModel.Toggle | ItemSelectionModel.Rows)
And now I have found out that selections made this way don't survive the filter proxy so I need to do my own logic anyway.
Thanks for the learning.