Advertisement
TheOldHunter

PySide2 QTreeView with Custom QSortFilterProxyModel

Jun 14th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.31 KB | None | 0 0
  1. from Qt import QtWidgets, QtCore
  2.  
  3.  
  4. class CustomProxyModel(QtCore.QSortFilterProxyModel):
  5.     def __init__(self):
  6.         super(CustomProxyModel, self).__init__()
  7.         # self.setDynamicSortFilter(True)
  8.  
  9.     def rowCount(self, index):
  10.         model_index = self.mapToSource(index)
  11.         if not model_index.isValid():
  12.             return 0
  13.         new_rc = self.sourceModel().rowCount(model_index) + 1
  14.         # self.insertRows(0)
  15.         return new_rc
  16.  
  17.     def insertRows(self, position, rows=1, parent=QtCore.QModelIndex()):
  18.         self.beginInsertRows(parent, position, position + rows - 1)
  19.         for row in range(rows):
  20.             self.insertRows(position, row, parent)
  21.             self.setData(parent, "AWESOME")
  22.             print 'AWESOME VIRTUAL ROW'
  23.         self.endInsertRows()
  24.         return True
  25.  
  26. class MyWindow(QtWidgets.QDialog):
  27.     k_base_path = '/home/harshad/skin_weights/'
  28.  
  29.     def __init__(self):
  30.         super(MyWindow, self).__init__()
  31.  
  32.         self.create_widgets()
  33.         self.create_connections()
  34.  
  35.     def create_widgets(self):
  36.         # Layout
  37.         layout = QtWidgets.QVBoxLayout()
  38.         self.setLayout(layout)
  39.  
  40.         # TreeView
  41.         self.view = QtWidgets.QTreeView()
  42.         layout.addWidget(self.view)
  43.  
  44.         # Selection model
  45.         self.selection_model = QtCore.QItemSelectionModel()
  46.         self.view.setSelectionModel(self.selection_model)
  47.  
  48.         # Model
  49.         self.model = QtWidgets.QFileSystemModel()
  50.         self.model.setRootPath(QtCore.QDir.rootPath())
  51.  
  52.         # Set Model Filters
  53.         self.model.setNameFilterDisables(False)
  54.         self.model.setNameFilters(["*.xml"])
  55.  
  56.         # Proxy
  57.         self.proxy = CustomProxyModel()
  58.         self.proxy.setSourceModel(self.model)
  59.         self.proxy.sort(0, QtCore.Qt.AscendingOrder)
  60.  
  61.         # View--Proxy--Model
  62.         self.view.setModel(self.proxy)
  63.         self.model_index = self.model.index(self.k_base_path)
  64.         self.proxy_index = self.proxy.mapFromSource(self.model_index)
  65.         self.view.setRootIndex(self.proxy_index)
  66.  
  67.         # Filters
  68.         self.view.hideColumn(1)
  69.         self.view.hideColumn(2)
  70.         self.view.setSortingEnabled(True)
  71.         self.view.setAlternatingRowColors(True)
  72.         # self.view.sortByColumn(0, QtCore.Qt.AscendingOrder)
  73.         self.view.header().setStretchLastSection(False)
  74.         self.view.header().resizeSection(0, 200)
  75.         # self.view.header().setSectionResizeMode(
  76.         #     0, QtWidgets.QHeaderView.Stretch)
  77.         # # self.view.header().setSectionResizeMode(
  78.         # #     1, QtWidgets.QHeaderView.ResizeToContents)
  79.         # self.view.header().setSectionResizeMode(
  80.         #     3, QtWidgets.QHeaderView.ResizeToContents)
  81.  
  82.         # self.proxy.setData(self.proxy.index(0,1), "AWESOME")
  83.  
  84.     def create_connections(self):
  85.         self.view.selectionModel().selectionChanged.connect(
  86.             self.on_selection_change)
  87.  
  88.     def on_selection_change(self):
  89.         view_index = self.view.selectionModel().selectedIndexes()[0]
  90.         proxy_index = self.proxy.mapToSource(view_index)
  91.         item_name = self.model.fileName(proxy_index)
  92.         print item_name
  93.  
  94. if __name__ == "__main__":
  95.     try:
  96.         win.close()
  97.         win.deleteLater()
  98.     except Exception:
  99.         pass
  100.  
  101.     win = MyWindow()
  102.     win.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement