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. TableView Multiple rows selection
Qt 6.11 is out! See what's new in the release blog

TableView Multiple rows selection

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 563 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.
  • D Offline
    D Offline
    davidesalvetti
    wrote on last edited by
    #1

    Hi all,

    I'm trying to introduce a multiple selection on my QML TableView but with no success. I managed to select only one row, setting the background color of the row.
    This is my current code:

            TableView {
                id: tableViewid
                columnWidthProvider: function (column) {
                    if (column === 0)
                        return 0;
    
                    return headerItems.itemAt(column).implicitWidth + 10
                }
                rowHeightProvider: function (column) { /*return 60;*/ }
                topMargin: columnsHeader.implicitHeight
    
                model: masterController.ui_databaseController.ui_tableModel
                clip: true
    
                property int selectedRow: -1
                property var deletingRow: []
                delegate: Rectangle {
                    id: modelrect
                    implicitWidth: textId.implicitWidth + 50
                    implicitHeight: textId.implicitHeight + 10
    
                    Text {
                        id: textId
                        text: display
                        anchors.fill: parent
                        color: row == tableViewid.selectedRow ? 'white' : 'black'
                        font.bold: row == tableViewid.selectedRow
                        font.pixelSize: Style.tables.valuePointSize
                        verticalAlignment: Text.AlignVCenter
                        horizontalAlignment: Text.AlignHCenter
                    }
                    color: setRowColor()
                    function setRowColor() {
                        console.log("Entering func")
                        if (flickableItemId.state == "base") {
                            console.log("State is this one")
                            if (row == tableViewid.selectedRow)
                                return Style.tables.selectedColor
                        } else if (flickableItemId.state == "deleting") {
                            console.log("Index ")
                            for (let i = 0; i < tableViewid.deletingRow.length; i++) {
                                if (tableViewid.deletingRow[i] == row)
                                    return "red"
                            }
                        }
    
                        return Style.tables.tableColor
                    }
    
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            if (flickableItemId.state == "base") {
                                if (tableViewid.selectedRow == row)
                                    tableViewid.selectedRow = -1
                                else
                                    tableViewid.selectedRow = row
                            } else if (flickableItemId.state == "deleting") {
                                let idx = tableViewid.deletingRow.indexOf(row)
                                if (idx > -1) {
                                    tableViewid.deletingRow.splice(idx, 1)
                                } else {
                                    tableViewid.deletingRow.push(row)
                                }
                                console.log(tableViewid.deletingRow)
                            }
                        }
                    }
    

    Basically, depending on the state of a component, I want to have a single selection or a multiple selection of the rows. To select a row I set a color of the Rectangle element.
    The problem I am facing is that when I am selecting only one row the color of the rectangles refresh correctly and I can see the row selected. When I want a multiple selection, when I click on the row, I can see that the row is inserted inside the array deletingRow, but the function setRowColor is not called, while it is called when I am in the single selection method.

    Anyone can help me understand why it is behaving like that?
    I am on windows, Qt 5.15.2 MinGw and I am using QtQuick.controls 2.
    Thanks in advance.

    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