我是模型视图新手,我一直在关注本教程在检查文档的同时,我偶然发现了这个小细节:可以下载的教程代码hereQAbstractItemModel 类(此处为 QAbstractListModel)中有 setData 方法,其代码为:
def setData(self, index, value, role = QtCore.Qt.EditRole):
if role == QtCore.Qt.EditRole:
row = index.row()
color = QtGui.QColor(value)
if color.isValid():
self.__colors[row] = color
self.dataChanged.emit(index, index)
return True
return False
根据教程中的解释以及我从文档中理解的内容,如果函数返回 True,则视图会更新,如果返回 false,则不会发生任何情况,但是当我将代码更改为:
def setData(self, index, value, role = QtCore.Qt.EditRole):
if role == QtCore.Qt.EditRole:
row = index.row()
color = QtGui.QColor(value)
if color.isValid():
self.__colors[row] = color
self.dataChanged.emit(index, index)
return False # This is what I changed in the code
return False
我意识到,如果 color.isValid() ,即使函数返回 False,视图仍然会更新。我是否误解了 setData 方法中的返回角色或者这是一个错误?
作为参考,我使用的是 PySide 1.2.1,而不是 PyQt4。