Code is almost complete. Here's the deal:
It is python and PySide. I have a QAbstractTableModel and a QTableView.
I cant get deleting rows correctly. I think the problem is somewhere in the indexes of the rows onde I delete one of them...
here is the button delegate I use:
class ButtonDelegate(QItemDelegate):
def __init__(self, parent):
QItemDelegate.__init__(self, parent)
def paint(self, painter, option, index):
widget = QWidget()
layout = QHBoxLayout()
widget.setLayout(layout)
btn = QPushButton("X")
btn.clicked.connect(partial(self.parent().cellButtonClicked, index))
layout.addWidget(btn)
layout.setContentsMargins(2,2,2,2)
if not self.parent().indexWidget(index):
self.parent().setIndexWidget(index, widget)
here's the cellButtonClicked method, it is under the table view:
class Table(QTableView):
def __init__(self, *args, **kwargs):
QTableView.__init__(self, *args, **kwargs)
self.setItemDelegateForColumn(6, ButtonDelegate(self))
self.setItemDelegateForColumn(0, EmptyDelegate(self))
self.setSortingEnabled(True)
def cellButtonClicked(self, index, *args):
model = self.model()
model.removeRow(index.row())
and here is the model removeRow Method:
def removeRow(self, row, parent = QtCore.QModelIndex()):
self.beginRemoveRows(parent, row, row)
array = []
for i in range(7):
if i == 0:
array.append(self.index(row, i).data())
else:
array.append(str(self.index(row, i).data()))
self.cycles.remove(array)
self.endRemoveRows()
# update custom node in maya.
self.getData()
I think that, mainly, the problem is that when I delete a row it does not update the indexes of the model. So when I click again in any delete button it starts de removeRow() with an index the does no match the rowCount of the model anymore, therefore I can't build the array to be removed from the model data.
Did it make sense? if you need more code, tell me what you need.
The problem is caused because you have set the value of the row when you have created each delegate, so its value is not updated.
A possible solution is to use a lambda function to pass a
QPersistenModelIndex
associated with the temporaryQModelIndex
, but I have seen that there is an unexpected behavior that is creating a selection, so I calledclearSelection()
.It is not necessary to connect to the cellButtonClicked slot since you can directly access the model using QModelIndex or QPersistenModelIndex.
Another option is to handle the clicked events through
editorEvent
since the providedQModelIndex
has updated values as shown below:In addition to this it is not necessary to iterate through data(), we can delete the row directly:
In the following link both options are implemented.