Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeView with QSortFilterProxyModel displays an empty row after changing the QAbstractItemModel
Forum Updated to NodeBB v4.3 + New Features

QTreeView with QSortFilterProxyModel displays an empty row after changing the QAbstractItemModel

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 5.1k Views 1 Watching
  • 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 Offline
    L Offline
    loki42
    wrote on last edited by
    #1

    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
    0
    • francescmmF Offline
      francescmmF Offline
      francescmm
      wrote on last edited by
      #2

      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
      0
      • L Offline
        L Offline
        loki42
        wrote on last edited by
        #3

        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
        0
        • francescmmF Offline
          francescmmF Offline
          francescmm
          wrote on last edited by
          #4

          Ok! I wait for the code! :)

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kernald
            wrote on last edited by
            #5

            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
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved