QAbstractItemModel and inserting rows and columns

  • Hello,

    I have sort of a general question about inserting new rows into a model after it has already been loaded. I have an editable model derived from QAbstractItemModel and it is initially loaded with rows all having 4 columns. I then want to add a row with 4 columns to a parent that already has existing rows. Is there a way using the standard interface for QAbstractItemModel to do this? If I do an insertRow() with a following insertColumn() then won't the existing rows under the parent get columns inserted as well (resulting in too many columns for those rows) ?

    I've tried a "roll my own" solution that inserts a row between calls to beginInsertRow() and endInsertRow() and then inserts the columns for that row between the beginInsertColumns() and endInsertColums(). Unfortunately, My view behaves as if there is only a single column. I'm wondering if there is a problem doing this as it is sending signals implying that the columns have changed for all the rows under the parent but they have only changed for the one row.

    Any suggestions or insight appreciated.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you trying to build some sort of tree model ?

  • Without looking in the source code I can't say for sure, but it is fairly possible that you can't call beginInsertColumns within beginInsertRow - endInsertRow block since they might use the same structure to keep data passed at beginInsertColumns and beginInsertColumns.
    especially counting that documentation states that end... functions have to be called immediately afterwards.

    If you do a lot of changes you might consider using beginResetModel endResetModel instead.

  • Well, it seems I am able to get past the problem I was having. I think the thing that actually fixed my problem (the view not showing all the columns for a row inserted under a parent not having any rows) was not implementing the insertColumns() properly. The case where you can use the insertColumns() to create a row with N columns under a parent without rows. In this case I needed to have both the beginInsertRows() / endInsertRows() signaling and then the beginInsertColumns() / endInsertColumns() signaling.

    Thanks for bearing with me and your advice!

Log in to reply