Wrap TreeWidget's invisible root item so that child items will receive tree change notifications

This commit is contained in:
Luke Campagnola 2017-07-28 16:05:58 -07:00
parent 6c7e0fae8e
commit 3fbc3864f2

View File

@ -168,7 +168,6 @@ class TreeWidget(QtGui.QTreeWidget):
for i in xrange(item.childCount()): for i in xrange(item.childCount()):
TreeWidget.informTreeWidgetChange(item.child(i)) TreeWidget.informTreeWidgetChange(item.child(i))
def addTopLevelItem(self, item): def addTopLevelItem(self, item):
QtGui.QTreeWidget.addTopLevelItem(self, item) QtGui.QTreeWidget.addTopLevelItem(self, item)
self.informTreeWidgetChange(item) self.informTreeWidgetChange(item)
@ -209,6 +208,11 @@ class TreeWidget(QtGui.QTreeWidget):
#for item in items: #for item in items:
#self.informTreeWidgetChange(item) #self.informTreeWidgetChange(item)
def invisibleRootItem(self):
# wrap this item so that we can propagate tree change information
# to children.
return InvisibleRootItem(QtGui.QTreeWidget.invisibleRootItem(self))
def itemFromIndex(self, index): def itemFromIndex(self, index):
"""Return the item and column corresponding to a QModelIndex. """Return the item and column corresponding to a QModelIndex.
""" """
@ -334,3 +338,46 @@ class TreeWidgetItem(QtGui.QTreeWidgetItem):
elif (role in (QtCore.Qt.DisplayRole, QtCore.Qt.EditRole) and text != self.text(column)): elif (role in (QtCore.Qt.DisplayRole, QtCore.Qt.EditRole) and text != self.text(column)):
treewidget.sigItemTextChanged.emit(self, column) treewidget.sigItemTextChanged.emit(self, column)
class InvisibleRootItem(QtGui.QTreeWidgetItem):
"""Wrapper around a TreeWidget's invisible root item that calls
TreeWidget.informTreeWidgetChange when child items are added/removed.
"""
def __init__(self, item):
self._real_item = item
def addChild(self, child):
self._real_item.addChild(child)
TreeWidget.informTreeWidgetChange(child)
def addChildren(self, childs):
self._real_item.addChildren(childs)
for child in childs:
TreeWidget.informTreeWidgetChange(child)
def insertChild(self, index, child):
self._real_item.insertChild(index, child)
TreeWidget.informTreeWidgetChange(child)
def insertChildren(self, index, childs):
self._real_item.addChildren(index, childs)
for child in childs:
TreeWidget.informTreeWidgetChange(child)
def removeChild(self, child):
self._real_item.removeChild(child)
TreeWidget.informTreeWidgetChange(child)
def takeChild(self, index):
child = self._real_item.takeChild(index)
TreeWidget.informTreeWidgetChange(child)
return child
def takeChildren(self):
childs = self._real_item.takeChildren()
for child in childs:
TreeWidget.informTreeWidgetChange(child)
return childs
def __getattr__(self, attr):
return getattr(self._real_item, attr)