PDA

View Full Version : tableview with abstractmodel



drmacro
2nd November 2017, 18:55
Every time I think I understand how to implement a tableview with a model...
:(

In this case I have two things:

when I insert a row in the model I've implemented the insert in the model:



def insertRows(self, row, count):
targetindex = self.createIndex(row, 0)
self.beginInsertRows(targetindex, self.rowCount(),self.rowCount()+1)
# copy last row data, except add a new StageState for all StageState columns

newrow = self.arraydata[self.rowCount()-1][:] # slice [:] clones the row
# new uuid for this character
newrow[0] = '{}'.format(uuid.uuid4())
self.arraydata.append(newrow)
self.endInsertRows()
return True


This works and the table IS updated with the new row at the correct place in the table.
self.arraydata is a list...so I don't get why the new row isn't at the end of the table.
(probably my limited python-ness :p )

Also, to delete a row, I implemented:



def removeRows(self, row, count):
targetindex =self.createIndex(row, 0)
self.beginRemoveRows(targetindex, row, row-1)
popped_item = self.arraydata.pop(targetindex.row())
self.endRemoveRows()
return True


This works fine for any row in the table, except the last row.
It does delete the last row, but, when I click on any other row after the delete:

QAccessibleTable::child: Invalid index at: 29 2
Cannot creat accessible child interface for object: QTableView(0x249a530, name = "table_cast") index: 123

In this particular run the table was 30 rows long and one was removed. I think that's the 29. I have no idea what index: 123 means...since there was/is only 30 rows...
:confused:

Regards,
Mac

d_stranz
2nd November 2017, 23:41
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 )).

drmacro
3rd November 2017, 18:10
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 )).

Thank you! I had forgotten about the flat-ness of the table models. And, had indeed misinterpreted the row indexes to be given to begin...Rows() methods.
The "-" indexes was me being sloppy.
The conversions of indexes to ModelIndexes was copied code from past attempts where I thought it was telling me an index object was needed not an integer...so, me lost in a fog, again.

The insert happening in the right places, even though I couldn't see how the insertRows() method could be putting there...is really embarrassing :o I had circumvented the method all together elsewhere in the code.