Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QTreeView with QSortFilterProxyModel displays an empty row after changing the QAbstractItemModel

    General and Desktop
    3
    5
    4544
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • L
      loki42 last edited by

      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
      this->endInsertRows();
      @

      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.

      1 Reply Last reply Reply Quote 0
      • francescmm
        francescmm last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • L
          loki42 last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • francescmm
            francescmm last edited by

            Ok! I wait for the code! :)

            1 Reply Last reply Reply Quote 0
            • K
              Kernald last edited by

              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.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post