Results 1 to 6 of 6

Thread: How can I properly implement QSortFilterProxyModel.parent to handle a virtual column?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2021
    Location
    London, UK
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question How can I properly implement QSortFilterProxyModel.parent to handle a virtual column?

    I have the following working code, which opens a QFileDialog with an extra column that shows the file name again (pointless, I know, but it’s a result of simplifying my issue):

    Qt Code:
    1. from PySide2 import QtCore, QtWidgets
    2.  
    3.  
    4. class MyProxyModel(QtCore.QSortFilterProxyModel):
    5.  
    6. def __init__(self, parent=None):
    7. super(MyProxyModel, self).__init__(parent)
    8. self._parents = {}
    9.  
    10. def mapToSource(self, index):
    11. if index.column() == 4:
    12. return QtCore.QModelIndex()
    13. return super(MyProxyModel, self).mapToSource(index)
    14.  
    15. def columnCount(self, index):
    16. return 5
    17.  
    18. def data(self, index, role=QtCore.Qt.DisplayRole):
    19. if role == QtCore.Qt.DisplayRole and index.column() == 4:
    20. return self.index(index.row(), 0, self._parents[index]).data(role)
    21. return super(MyProxyModel, self).data(index, role)
    22.  
    23. def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
    24. if section == 4 and orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
    25. return 'My Column'
    26. return super(MyProxyModel, self).headerData(section, orientation, role)
    27.  
    28. def index(self, row, column, parent=QtCore.QModelIndex()):
    29. if column == 4:
    30. index = self.createIndex(row, column)
    31. self._parents[index] = parent
    32. return index
    33. return super(MyProxyModel, self).index(row, column, parent)
    34.  
    35. def parent(self, index):
    36. if index.column() == 4:
    37. return QtCore.QModelIndex()
    38. return super(MyProxyModel, self).parent(index)
    39.  
    40.  
    41. QtWidgets.QApplication([])
    42. dialog = QtWidgets.QFileDialog()
    43. dialog.setOption(dialog.DontUseNativeDialog, True)
    44. dialog.setProxyModel(MyProxyModel(dialog))
    45. dialog.exec_()
    To copy to clipboard, switch view to plain text mode 

    As you can see, parent() is returning an invalid index for items of column 4, and instead I’m retrieving the actual parent inside data(), which isn’t ideal. But if I try the following, it exits with an access violation:

    Qt Code:
    1. (...)
    2. def data(self, index, role=QtCore.Qt.DisplayRole):
    3. if role == QtCore.Qt.DisplayRole and index.column() == 4:
    4. # Either return causes access violation.
    5. return self.index(index.row(), 0, self.parent(index)).data(role)
    6. return self.index(index.row(), 0, index.parent()).data(role)
    7. return index.sibling(index.row(), 0).data(role)
    8. return super(MyProxyModel, self).data(index, role)
    9. (...)
    10. def parent(self, index):
    11. if index.column() == 4:
    12. return self._parents[index]
    13. return super(MyProxyModel, self).parent(index)
    14. (...)
    To copy to clipboard, switch view to plain text mode 

    I also tried leveraging QModelIndex’s internal pointer, with the same result (access violation):

    Qt Code:
    1. # No __init__() defined; data() exactly like above.
    2. (...)
    3. def index(self, row, column, parent=QtCore.QModelIndex()):
    4. if column == 4:
    5. return self.createIndex(row, column, parent)
    6. return super(MyProxyModel, self).index(row, column, parent)
    7.  
    8. def parent(self, index):
    9. if index.column() == 4:
    10. return index.internalPointer()
    11. return super(MyProxyModel, self).parent(index)
    12. (...)
    To copy to clipboard, switch view to plain text mode 

    Pretty sure I’m missing something, but I can’t figure out what it is…
    Last edited by Unai; 1st September 2021 at 17:11.

Similar Threads

  1. Replies: 1
    Last Post: 14th June 2011, 15:50
  2. QSortFilterProxyModel not filtering properly
    By freemind in forum Qt Programming
    Replies: 9
    Last Post: 8th August 2010, 01:23
  3. Any way to properly handle Windows DPI setting?
    By vaddimka in forum Qt Programming
    Replies: 15
    Last Post: 16th May 2010, 10:03
  4. Unable to properly implement a QRubberBand
    By jamadagni in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2009, 12:39

Tags for this Thread

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.