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. Table selection not working
Forum Update on Monday, May 27th 2025

Table selection not working

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 61 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.
  • M Offline
    M Offline
    Mlibu
    wrote last edited by Mlibu
    #1

    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.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jhayar
      wrote last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • J jhayar

        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

        M Offline
        M Offline
        Mlibu
        wrote last edited by Mlibu
        #3

        @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.

        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