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. QSortFilterProxyModel: inconsistent changes reported by source model
Qt 6.11 is out! See what's new in the release blog

QSortFilterProxyModel: inconsistent changes reported by source model

Scheduled Pinned Locked Moved General and Desktop
17 Posts 3 Posters 17.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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #7

    Are you correctly calling beginRemoveRows() and endRemoveRows() in your model implementation? These are needed to notify any connected proxies and views that the source model is changing.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #8

      In source model:
      @
      bool CatalogTableModel::removeRows (int row, int count, const QModelIndex &parent)
      {
      //TODO:
      Q_UNUSED (parent);
      beginRemoveRows(QModelIndex(), row, row + count - 1);
      //int column = row + count;
      for (int i = 0; i < count; ++i)
      {
      delete listItem->at(row);
      listItem->removeAt(row);
      }
      emit layoutChanged();
      endRemoveRows();
      return true;
      }
      @
      I don't sure correctly it is or not..

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #9

        I don't think that you need to emit the layoutChanged() signal. The {begin|end}RemoveRows() functions do everything that is needed for you. Try removing that emit end see if it works then.

        I suspect what is happening is that the proxy and views are receiving signals from you and from {begin|end}RemoveRows() in an unexpected order which causes them to misbehave.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #10

          With out emit the layoutChanged() programm to crash!
          Maybe link between source model and proxy is not correct?

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #11

            Setting a proxy is a one-liner so I doubt it is that. I suspect you have a problem in your model. I suggest that you run your model with the qmodeltest helper which diagnoses common model implementation problems. This "wiki article":http://developer.qt.nokia.com/wiki/Model_Test describes how to use qmodeltest and the source can be downloaded from "gitorious":https://qt.gitorious.org/qt/qt/trees/4.7/tests/auto/modeltest.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #12

              Let's me stupid question - gitotious of model test have pro-file, but wiki article say about pri-file. How I can get pri file?

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #13

                Ah the instructions must be slightly out of date wrt the code. It used to be in it's own repository on Qt-labs. I think you can just copy the modeltest.{h,cpp} files to a little test project and instatiate them as shown in the above wiki article.

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #14

                  I use modeltest and have next message:

                  ratbr QModelIndex(-1,-1,0x0,QObject(0x0) ) 73 73
                  rr QModelIndex(-1,-1,0x0,QObject(0x0) ) 73 73

                  and other simular.
                  Is it Ok? I check mapping index from proxy to source - it ok.
                  What else I may do?

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #15

                    Debug your removeRows() function to make sure it is working properly. Step through it in a debugger to see where it crashes and why.

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #16

                      I get error:
                      @
                      void QSortFilterProxyModelPrivate::source_items_removed(
                      const QModelIndex &source_parent, int start, int end, Qt::Orientation orient)
                      {
                      if ((start < 0) || (end < 0))
                      return;
                      IndexMap::const_iterator it = source_index_mapping.constFind(source_parent);
                      if (it == source_index_mapping.constEnd()) {
                      // Don't care, since we don't have mapping for this index
                      return;
                      }

                      Mapping *m = it.value();
                      QVector<int> &source_to_proxy = (orient == Qt::Vertical) ? m->proxy_rows : m->proxy_columns;
                      QVector<int> &proxy_to_source = (orient == Qt::Vertical) ? m->source_rows : m->source_columns;
                      
                      if (end >= source_to_proxy.size())
                          end = source_to_proxy.size() - 1;
                      
                      // Shrink the source-to-proxy mapping to reflect the new item count
                      int delta_item_count = end - start + 1;
                      source_to_proxy.remove(start, delta_item_count); //This crash
                      

                      @

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #17

                        As ZapB said
                        [quote author="ZapB" date="1310123242"]Setting a proxy is a one-liner[/quote]unless you must overwrite a standard function of QSortFilterProxyModel.

                        Normally removing rows will be done in the sourcemodel. As you do a selection in the view you have to map the indexes getting from the view to the source with
                        @// single selection
                        QModelIndex SourceIndex = tableProxyModel->mapToSource(viewIndex);

                        // multiple selection
                        QModelIndexList SourceIndexList;
                        for (int i=0; i<viewIndexList.length(); i++)
                        {
                        SourceIndexList.append(tableProxyModel->mapToSource(viewIndexList[i]));
                        }
                        @

                        Also, if you have multiple selection you must sort the indexlist before removing like this:
                        @void CatalogTableModel::removeSelectedRows(const QModelIndexList &indexlist)
                        {
                        QList<int> rows;
                        foreach(const QModelIndex & index, indexlist)
                        {
                        rows.append(index.row());
                        }

                        qSort(rows);
                        
                        int prev = -1;
                        for(int i=rows.count()-1; i>=0; i-=1 )
                        {
                            int current = rows[i];
                            if(current != prev)
                            {
                                QModelIndex _index = createIndex(current, 0);
                                removeLabel(_index);
                                prev = current;
                            }
                        }
                        

                        }
                        @

                        Cheers,
                        Thomas

                        Edit: Uuups, little bit late ...

                        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