这些情况的解决方案是使用QIdentialProxy模型,但PySide和PySide中不存在此类,因此我们必须使用类似的类,如QSortFilterProxy模型,并覆盖headerData方法。
...
class HeaderProxyModel(QSortFilterProxyModel):
def __init__(self, *args, **kwargs):
QSortFilterProxyModel.__init__(self, *args, **kwargs)
self.labels = []
def setHeaderLabels(self, labels):
self.labels = labels
def headerData(self, section,orientation, role = Qt.DisplayRole):
if orientation == Qt.Horizontal and 0 <= section < self.columnCount() and role==Qt.DisplayRole and section < len(self.labels) :
return self.labels[section]
return QSortFilterProxyModel.headerData(self, section, orientation, role)
class TreeViewB(QTreeView):
'''
THIS VIEW Needs the Headers:
Item Name | Multiply
'''
def __init__(self, sourceView):
super(TreeViewB, self).__init__()
model = sourceView.model()
proxy = HeaderProxyModel()
proxy.setSourceModel(model)
proxy.setHeaderLabels(["Name", "Multiply"])
self.setModel(proxy)
self.setRootIndex(proxy.index(0,0))
...