QTreeView with QSortFilterProxyModel displays an empty row after changing the QAbstractItemModel

  • Hi all,

    i have build a cutsom TreeModel derived from QAbstractItemModel.
    This model is shown within a QTreeView widget.

    Everything works well, unless i add a QSortFilterProxyModel between the view and the model.

    If i add new rows within the model there is an additional empty row shown in the QTreeView.

    My row-insertion code in the model looks like that:

    this->beginInsertRows(QModelIndex() , rootItem->childCount(), rootItem->childCount() + 1);
    // insert the line

    If i add more elements, there are spawning more empty rows.

    Do i have to reemplement the QSortFilterProxyModel to add a proxy function for the insertion of new rows?

    I'm using Qt 4.8.4 on a linux system.

  • When you insert a QSortFilterProxyModel, you are passing the proxyModel to the QTreeView, so when you are inserting rows, in the model() of the QTreeView, what you are doing is inserting in the proxyModel.

    In addition, if you are passing as parent a new QModelIndex(), it will refer to the proxyModel.

    Consequently, you should reimplement the QSortFilterProxyModel, to add this method and create the casts from the proxyModel to the source model inside it. So, if your show more code I can help you better.

  • The model is "self-updating". The underlaying data is feteched via an API from third-party software.

    So i always edit the data within my model, not in the proxyModel. Thats why the effect is such strange.

    I will clean up the code and publish it somewhere, maybe i'm missing something.

  • Ok! I wait for the code! :)

  • The issue here is that you tell your model that you are inserting two rows:
    @this->beginInsertRows(QModelIndex() , rootItem->childCount(), rootItem->childCount() + 1);@
    According to http://qt-project.org/doc/qt-4.8/qabstractitemmodel.html#beginInsertRows, you have to specify the first and last row number. Which, if you add only one row, should be the same. Remove the +1, it will probably solve your issue.

    I had a similar problem with Qt 4.7, a failed count on beginInsertRows call, which was working fine until I added a proxy model.

Log in to reply