Important: Please read the Qt Code of Conduct -

[SOLVED] TableView remove all selected items

  • Hi,

    I can't figure out how to remove all selected items of a TableView, when using SelectionMode.ExtendedSelection.

    There is a forEach function in the Selection, but this only iterates over the selection from the begining. The problem is, when doing

    @tableView.selection.forEach( function(rowIndex) { model.remove(rowIndex) } )@

    the first item gets removed, and all following rowIndex values are wrong.

    Has someone a simple solution to this?


  • Lifetime Qt Champion


    You would need to start from the last selection element and go back to the first

    Hope it helps

  • I know, but what I don't know is how to sort the selection.

    Is there a way to get the range of the selection as an array?

    The problem is, that when you use SelectionMode.ExtendedSelection, you can select random items. They don't have to be consecutive.


  • Lifetime Qt Champion

    Are you sure that even if randomly selected, the order of the index returned by selection is the same ?

  • I have found a solution. You have to use the private __ranges array of the selection object.

    Here is my solution:

    function removeSelectedRows() {
    for (var i = selection.__ranges.length - 1 ; i >= 0 ; -- i) {
    var index = selection.__ranges[i][0];
    var count = 1 + selection.__ranges[i][1] - selection.__ranges[i][0];
    model.remove(index, count)

Log in to reply