Results 1 to 2 of 2

Thread: PyQt5 QSortFilterProxyModel: index from wrong model passed to mapFromSource + segfalt

  1. #1
    Join Date
    Feb 2018
    Posts
    2
    Thanked 1 Time in 1 Post
    Qt products
    PyQt3 PyQt4
    Platforms
    Unix/X11

    Default PyQt5 QSortFilterProxyModel: index from wrong model passed to mapFromSource + segfalt

    Hi,

    I'm having issue with what I believe to be an hello world example.

    I'm trying to implement a QSortFilterProxyModel to filter a Table which takes its data from a subclass of QAbstractTableModel.

    The problem that I have is that whenever I'm selecting a cell and then start filtering, I'm having a segmentation fault from the python interpreter. The stacktrace guides me to mapFromSource in a QT shared object. Sometime I see the following error in the terminal in which I'm running my QtApplication

    index from wrong model passed to mapFromSource

    I've checked the Qt Documentation and I'm following the recommendation when subclassing QAbstractTableModel and QSortFilterProxyModel.

    Here is my implementation of my QAbstractTableModel and QSortFilterProxyModel

    Qt Code:
    1. class MyTableModel(QAbstractTableModel):
    2. def __init__(self, datain, parent=None, *args):
    3. QAbstractTableModel.__init__(self, parent, *args)
    4. self.arraydata = datain
    5.  
    6. def rowCount(self, parent):
    7. return len(self.arraydata)
    8.  
    9. def columnCount(self, parent):
    10. return BankRecord.getMaxIdx()
    11.  
    12. def data(self, index, role):
    13. if not index.isValid():
    14. return QVariant()
    15. elif role != Qt.DisplayRole:
    16. return QVariant()
    17. return QVariant(self.arraydata[index.row()].getIdxData(index.column()))
    18.  
    19. def headerData(self, section, orientation, role):
    20. if orientation == QtCore.Qt.Vertical:
    21. return QVariant()
    22. if role != Qt.DisplayRole:
    23. return QVariant()
    24. return QVariant(BankRecord.getIdxHeaderData(section))
    25.  
    26. class RecordFilter(QSortFilterProxyModel):
    27. def __init__(self, parent):
    28. super(RecordFilter,self).__init__(parent)
    29. self.filterHeader = None
    30.  
    31. def setFilterHeader(self, filterHeader):
    32. self.filterHeader = filterHeader
    33.  
    34. def filterAcceptsRow(self, source_row, source_parent):
    35. sourceModel = self.sourceModel()
    36. if not self.filterHeader:
    37. return False
    38. for i in range(BankRecord.getMaxIdx()):
    39. idx = sourceModel.index(source_row, i, source_parent)
    40. dataString = sourceModel.data(idx, Qt.DisplayRole).value().lower()
    41. filterText = self.filterHeader.filterText(i).lower()
    42. if len(filterText) > 0 and not dataString.find(filterText) >= 0:
    43. return False
    44. return True
    To copy to clipboard, switch view to plain text mode 

    If you are curious, the rest of the source code of my application is available on Github (https://github.com/lcarlier/BankRecordsAnalyzer)
    You can easily test the problem because there is a record directly inserted into the table. Then you must play a bit with the filter (adding removing characters in filter fields) and the application is eventually crashing.

    Has anybody an idea what my problem is?

    Kind regards,

    Laurent

  2. #2
    Join Date
    Feb 2018
    Posts
    2
    Thanked 1 Time in 1 Post
    Qt products
    PyQt3 PyQt4
    Platforms
    Unix/X11

    Default Re: PyQt5 QSortFilterProxyModel: index from wrong model passed to mapFromSource + seg

    Hi,
    I received a personal mail from a person who gave me the answer.
    I'm posting it here so that it can help other person if needed.
    The problem that I had is that whenever my filter was updated I was calling the method
    Qt Code:
    1. tableModel.layoutChanged.emit()
    To copy to clipboard, switch view to plain text mode 
    iso of the method
    Qt Code:
    1. proxy.invalidateFilter()
    To copy to clipboard, switch view to plain text mode 
    Thanks to Norman Brown for contacting me and giving me that answer.
    Kind regards,
    Laurent

  3. The following user says thank you to lcarlier for this useful post:

    d_stranz (19th March 2018)

Similar Threads

  1. in QAbstractItemView rowsAboutToBeRemoved() give wrong model index
    By moh.gup@gmail.com in forum Qt Programming
    Replies: 2
    Last Post: 15th July 2012, 16:29
  2. Replies: 0
    Last Post: 8th December 2011, 02:00
  3. Replies: 1
    Last Post: 6th October 2011, 01:13
  4. Replies: 0
    Last Post: 21st June 2011, 19:27
  5. Replies: 0
    Last Post: 7th October 2010, 20:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.