1
如何在編輯QTableView單元格時保持其原始值?當我開始編輯單元格時,它會自動清除。我搜遍了所有的地方,我找不到任何會導致某處的指針。我的實現模型視圖:編輯QTableView單元格值
class BlockViewModel(QAbstractTableModel):
def __init__(self, structure, data):
QAbstractTableModel.__init__(self)
self._data = data
self._struct = structure
for i, s in enumerate(structure):
cmnt = s['comment']
name = cmnt if cmnt else s['name']
self.setHeaderData(i, Qt.Horizontal, name)
def rowCount(self, parent=QModelIndex()):
return len(self._data)
def columnCount(self, parent = QModelIndex()):
return len(self._struct)
def data(self, index, role):
if role == Qt.DisplayRole:
try:
row = index.row()
col = index.column()
name = self._struct[col]['name']
return self._data[row][name]
except:
pass
elif role == Qt.CheckStateRole:
return None
return None
def flags(self, index):
flags = super(self.__class__,self).flags(index)
flags |= Qt.ItemIsEditable
flags |= Qt.ItemIsSelectable
flags |= Qt.ItemIsEnabled
flags |= Qt.ItemIsDragEnabled
flags |= Qt.ItemIsDropEnabled
return flags
def headerData(self, section, orientation, role = Qt.DisplayRole):
if role != Qt.DisplayRole:
return None
if orientation == Qt.Horizontal:
cmnt = self._struct[section]['comment']
return cmnt if cmnt else self._struct[section]['name']
else:
return str(section)
def setData(self, index, value, role=Qt.EditRole):
row = index.row()
col = index.column()
name = self._struct[col]['name']
self._data[row][name] = value
self.emit(SIGNAL('dataChanged()'))
return True
太好了,謝謝! Qt文檔在這方面有點欠缺。 – marrat