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 mapToSource causes segfault
Forum Updated to NodeBB v4.3 + New Features

QSortFilterProxyModel mapToSource causes segfault

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 486 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.
  • ocgltdO Offline
    ocgltdO Offline
    ocgltd
    wrote on last edited by ocgltd
    #1

    My tableView hides rows that are not in a cache held by the data model, and in order to hide the row. My TableView connects to a QSortFilterProxyModel which connects to a QAbstractTableModel. The function is as follows:

    bool CompanySortFilterProxyModel::isRowInCache(const int rowNumber) const {
        QModelIndex sourceModelIndex = mapToSource(createIndex(rowNumber,0));  // Crash on this line
        return m_dataModel->isRowInCache(sourceModelIndex.row());
    }
    

    I pass a rowNumber to the above function (in this case row 0), and my TableView holds 16 rows, my data model holds 100+ rows. The above function must translate the QSFPM row number to a QATM row number, so I call the mapToSource function. But the mapToSource function segfaults in the ancestor's index_to_iterator function.

    What am I doing wrong? With qDebug I confirmed the sourceModelIndex is valid. The QSFPM code which segfaults is below.

    inline QHash<QModelIndex, Mapping *>::const_iterator index_to_iterator(
          const QModelIndex &proxy_index) const
      {
          Q_ASSERT(proxy_index.isValid());
          Q_ASSERT(proxy_index.model() == q_func());
          const void *p = proxy_index.internalPointer();
          Q_ASSERT(p);
          QHash<QModelIndex, Mapping *>::const_iterator it =
              static_cast<const Mapping*>(p)->map_iter;  // SEGFAULT HERE
          Q_ASSERT(it != source_index_mapping.constEnd());
          Q_ASSERT(it.value());
          return it;
      }
    
    ocgltdO 1 Reply Last reply
    0
    • ocgltdO ocgltd

      My tableView hides rows that are not in a cache held by the data model, and in order to hide the row. My TableView connects to a QSortFilterProxyModel which connects to a QAbstractTableModel. The function is as follows:

      bool CompanySortFilterProxyModel::isRowInCache(const int rowNumber) const {
          QModelIndex sourceModelIndex = mapToSource(createIndex(rowNumber,0));  // Crash on this line
          return m_dataModel->isRowInCache(sourceModelIndex.row());
      }
      

      I pass a rowNumber to the above function (in this case row 0), and my TableView holds 16 rows, my data model holds 100+ rows. The above function must translate the QSFPM row number to a QATM row number, so I call the mapToSource function. But the mapToSource function segfaults in the ancestor's index_to_iterator function.

      What am I doing wrong? With qDebug I confirmed the sourceModelIndex is valid. The QSFPM code which segfaults is below.

      inline QHash<QModelIndex, Mapping *>::const_iterator index_to_iterator(
            const QModelIndex &proxy_index) const
        {
            Q_ASSERT(proxy_index.isValid());
            Q_ASSERT(proxy_index.model() == q_func());
            const void *p = proxy_index.internalPointer();
            Q_ASSERT(p);
            QHash<QModelIndex, Mapping *>::const_iterator it =
                static_cast<const Mapping*>(p)->map_iter;  // SEGFAULT HERE
            Q_ASSERT(it != source_index_mapping.constEnd());
            Q_ASSERT(it.value());
            return it;
        }
      
      ocgltdO Offline
      ocgltdO Offline
      ocgltd
      wrote on last edited by
      #2

      @ocgltd I found StackOverFlow post which suggests using 'index' instead of 'createIndex' in my function. And that works.

      Can someone explain why? What's the difference?

      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