我需要一些有关树视图的帮助。当我将模型指定为
class TreeModel(QtGui.QStandardItemModel):
def __init__(self,parent=None):
QtGui.QStandardItemModel.__init__(self,parent)
self.rootItem = QtGui.QStandardItem('root')
我可以从窗口将项目添加到树中
def on_actionAddItem_triggered(self,checked=None):
if checked is None:
return
parent = self.model.invisibleRootItem()
parent.appendRow(QtGui.QStandardItem("test"))
但是当我尝试重载模型时,树视图不会更新。该代码无法更新树视图。有人可以解释一下吗?
class TreeModel(QtGui.QStandardItemModel):
def __init__(self,parent=None):
QtGui.QStandardItemModel.__init__(self,parent)
self.rootItem = QtGui.QStandardItem('root')
def data(self,index,role):
if role == QtCore.Qt.DisplayRole:
row = index.row()
text = self.rootItem.child(row)
return text.text()
def columnCount(self,parent=None):
return 1
def rowCount(self,parent=QtCore.QModelIndex()):
return self.rootItem.rowCount()
def headerData(self, column, orientation, role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
if column == 0:
return 'zero'