Reputation: 14506
I use PyQt to write a custom implementation of QTableView and QAbstractItemModel. I want to create a button, that removes a row from TableModel by calling re-implemented rowRemoved
event handler. For some reason, model attempts to create the row's header for removed row, which causes a crash.
Here are the implementations of removeRows
, headerData
and rowCount
. self.alignment.sequences
is the list of data, each header is filled with sequences.name
:
def removeRows(self, position, rows, parent=QModelIndex()):
print "removeRows called"
self.beginRemoveRows(parent, position, position + rows -1)
for i in range(int(rows)):
self.alignment.sequences.pop(position)
self.endRemoveRows()
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return QVariant(str(col))
elif orientation == Qt.Vertical and role == Qt.DisplayRole:
try:
return self.alignment.sequences[col].name
except:
print "Exception: no header with index %s" % col
def rowCount(self, parent):
return len(self.alignment.sequences)
For some reason, my application crashes. Its execution results in exception, caught in headerData
:
Exception: no header with index 16
GUI creates space for header, corresponding to removed row, but, obviously, fails to fill it.
Do you have any ideas, why Qt doesn't react to row removal appropriately? Thanks.
Upvotes: 1
Views: 773
Reputation: 6320
Just from a quick look, it seems like you data storage and headers are from the same variable. So you might have removed a header from remove Row. If you keep them as separate variables then your data manipulation wont affect your headers at all.
Another thing to do is to get rid of the Try Except with a generic exception. That way you can see what kind of error you are getting.
try:
return self.alignment.sequences[col].name
except (IndexError, AttributeError) as err:
print(err)
Upvotes: 1