Skip to content Skip to sidebar Skip to footer

How Do I Remove Items From Model In Python

How do I properly remove items from my custom QAbstractTableModel? Do i need to change this to QStandardItemModel instead? This is the before: This is the after...it leaves empty

Solution 1:

The reason for this behavior is that you're using the wrong row in beginRemoveRows(): you should use the row number you're removing, and since you're using rowCount() that row index is invalid.

defremoveItems(self, items):
        self.beginRemoveRows(QtCore.QModelIndex(), self.rowCount() - 1, self.rowCount() - 1)
        self.items = [x for x inself.items if x notin items]
        self.endRemoveRows()

To be more correct, you should remove the actual rows in the model. In your simple case it won't matter that much, but in case your model becomes more complex, keep in mind this.

defremoveItems(self, items):
        removeRows = []
        for row, item inenumerate(self.items):
            if item in items:
                removeRows.append(row)
        for row insorted(removeRows, reverse=True):
            self.beginRemoveRows(QtCore.QModelIndex(), row, row)
            self.items.pop(row)
            self.endRemoveRows()

The reason for the reversed row order in the for cycle is that for list consistency reasons the row removal should always begin from the bottom. This can be important if you want to remove rows arbitrarily while keeping the current selection in case the removed items are not selected.

That said, as already suggested in the comments, if you don't need specific behavior and implementation, creating a QAbstractItemModel (or any abstract model) subclass is unnecessary, as QStandardItemModel will usually be enough, as it already provides all required features (including drag and drop support, which can be rather complex if you don't know how the Qt data model works). Well, unless it's for learning purposes, obviously.

Post a Comment for "How Do I Remove Items From Model In Python"