我是模型视图的新手,我一直在跟踪
this tutorial,同时检查文档,我偶然发现了这个小细节:可以下载的教程的代码
here在QAbstractItemModel类(这里是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.