I have a QStandardItemModel that accomodates data. In one of the columns, I would like to add some QWidgets (clickable pictures). After adding QSortFilterProxyModel for soring/filtering purpose, however, the QSortFilterProxyModel hides my desired QWidgets.

I've searched the Internet but could not find how to keep the QWidget and QSortFilterProxyModel simultaneously. It would be much appreciated if someone may guide me on this issue. Thanks.

A minimal example, using QPushButton as my desired QWidget:

Qt Code:
  1. from PyQt5.QtWidgets import *
  2. from PyQt5.QtCore import *
  3. from PyQt5.QtGui import *
  4.  
  5. class Buttons(QWidget):
  6. def __init__(self):
  7. super().__init__()
  8. layout = QHBoxLayout(self)
  9. layout.addWidget(QPushButton('btn1'))
  10. layout.addWidget(QPushButton('btn2'))
  11. self.setLayout(layout)
  12.  
  13. class MainWindow(QMainWindow):
  14. def __init__(self, *args, **kwargs):
  15. super(MainWindow, self).__init__(*args, **kwargs)
  16. tab = QTableView()
  17. if True: # This shows the buttons in a cell
  18. tab.setModel(sti)
  19. else: # This does not show the buttons
  20. proxy.setSourceModel(sti)
  21. tab.setModel(proxy)
  22. sti.appendRow([QStandardItem(str(i)) for i in range(5)])
  23. tab.setIndexWidget(sti.index(0, 0), QPushButton("hi"))
  24. sti.appendRow([])
  25. tab.setIndexWidget(sti.index(1, 2), Buttons())
  26. self.setCentralWidget(tab)
  27.  
  28. app = QApplication([])
  29. window = MainWindow()
  30. window.resize(800, 600)
  31. window.show()
  32. app.exec_()
To copy to clipboard, switch view to plain text mode