In your removeRows() method, what happens if you want to remove row # 0? Where does the model look to find row # "-1" in line 3? If you are only removing 1 row, then I believe you should be using "row", "row" for both arguments to the beginRemoveRows() call, and they should be in increasing row index order if you are removing more than one row. Also doesn't make a lot of sense to use "row" to create an index, and then targetIndex.row() to retrieve it in line 4. It's just "row" number; it doesn't transmute into something special when you use it to create a model index.
Likewise for insertRows() - I think you should be using "self.rowCount()" for both arguments to the beginInsertRows() method if you are inserting only one row.
See the description in the docs for
QAbstractItemModel::beginInsertRows(). In the example given, they insert -two- rows into the model, so the arguments given are "row 1 index" through "row 2 index", which in the example are 4, 5. If only one row was inserted, it would have been 4, 4.
In all cases, you have misinterpreted the "parent" (first) argument to the begin...Rows() methods. Table models are flat; there are no parents - every row has an "invalid" parent (index.parent().isValid() returns false). Only tree models will have parents. So for this first argument, you should be passing an invalid model index (via QModelIndex() or the index resulting from createIndex( -1, -1 )).
Bookmarks