11

I want to delete a selected row from the table when I click on the delete button.

But I can't find anything regarding deleting rows in the Qt documentation. Any ideas?

Image

Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
laura
  • 2,085
  • 13
  • 36
  • 68

3 Answers3

15

You can use the bool QAbstractItemModel::removeRow(int row, const QModelIndex & parent = QModelIndex()) functionality for this.

Here you can find an example for all this.

Also, here is an inline quote from that documentation:

removeRows()

Used to remove rows and the items of data they contain from all types of model. Implementations must call beginRemoveRows() before inserting new columns into any underlying data structures, and call endRemoveRows() immediately afterwards.

The second part of the task would be to connect the button's clicked signal to the slot executing the removal for you.

László Papp
  • 51,870
  • 39
  • 111
  • 135
6

If you are removing multiple rows you can run into some complications using the removeRow() call. This operates on the row index, so you need to remove rows from the bottom up to keep the row indices from shifting as you remove them. This is how I did it in PyQt, don't know C++ but I imagine it is quite similar:

rows = set()
for index in self.table.selectedIndexes():
    rows.add(index.row())

for row in sorted(rows, reverse=True):
    self.table.removeRow(row)

Works perfectly for me! However one thing to know, in my case this function gets called when a user clicks on a specific cell (which has a pushbutton with an 'X'). Unfortunately when they click on that pushbutton it deselects the row, which then prevents it from getting removed. To fix this I just captured the row of the sender and appended it to the "remove_list" at the very beginning, before the "for loops". That looks like this:

rows.add(self.table.indexAt(self.sender().pos()).row())
Spencer
  • 1,931
  • 1
  • 21
  • 44
  • You're confusing [QTableView](https://doc.qt.io/qt-5/qtableview.html) (question) with [QTableWidget](https://doc.qt.io/qt-5/qtablewidget.html) (answer). QTableView has no method [selectedRanges()](https://doc.qt.io/qt-5/qtablewidget.html#selectedRanges), and addition/deletion/updates must be handled at the model level, not at the view level (QTableWidget has its own embedded model managed by the view) – mins Mar 01 '19 at 18:27
  • @mins Indeed you're right! The code is for that reason somewhat invalid, however the concept is much the same when working with a model. I'll update my answer to include an example for working on the model directly. It's actually a bit simple for a QTableView – Spencer Mar 01 '19 at 22:26
  • First, the argument to `sorted` is `reverse` and not `reversed`. Second, instead of calling `self.table.model().removeRow` you can call `self.table.removeRow`, which requires only the row index you want to delete. Third, the set of rows can be shortened with a set comprehension. – cbrnr Jun 26 '19 at 09:20
  • @cbmr thanks for pointing out those mistakes, also feel free to edit in the future. I've fixed the sort argument and took your suggestion on calling the `removeRow` method of the view. I didn't test this so I'm taking your word for it. Lastly while comprehensions are fancy, they are not so good for illustrating code examples - I prefer to write out the full loop for illustration. – Spencer Jul 01 '19 at 17:42
0

You can use another way by deleting the row from database, then clear the model and fill it again, this solution is also safe when you are removing multiple rows.

  • 13
    I think it would be best to avoid this kind of thinking. – g24l Nov 13 '15 at 14:46
  • 1
    Doesn't that kind of go against the whole MVC concept? What is so cool about Views is how they "dynamically" link to the database, and forcibly clearing it out every time and re-populating just defeats the whole purpose. – Spencer Mar 01 '19 at 22:27