我有点像PyQt4的新手,我只想编辑QListView中的项目。但是,当我双击该项目时,此项目将被清除。所以我必须再次输入整个条目,前提是我能记住我要修改的项目是什么。我错过了什么? 感谢您的宝贵帮助。 这是我的代码:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class MyModel(QAbstractListModel):
def __init__(self, parent):
QAbstractListModel.__init__(self, parent=None)
self.Parent=parent
self.listdata=[[QVariant(1),QString('My first Item'),QString('a remark'),QVariant(7),QVariant(0)],[QVariant(1),QString('My second Item'),QString('another remark'),QVariant(0),QVariant(0)]]
def rowCount(self,parent=QModelIndex()):
return len(self.listdata)
def columnCount(self, index=QModelIndex()):
return 5
def data(self, index, role):
if not index.isValid():
return QVariant()
if role == Qt.DisplayRole:
return QVariant(self.listdata[index.row()][1])
elif role == Qt.UserRole:
return QVariant(self.listdata[index.row()][0])
elif role == Qt.ToolTipRole:
return QVariant(self.listdata[index.row()][2])
elif role == Qt.ForegroundRole: #Qt.ForegroundRole & BackgroundRole BUG for Combobox (pyqt4, ubuntu 10.04)
color=self.listdata[index.row()][3].toInt()[0]
if color>0:
if color==6:
return(QColor(Qt.lightGray))
elif color==7:
return(QColor(Qt.red))
elif color==8:
return(QColor(Qt.green))
elif role == 33: #isDeleted
return QVariant(self.listdata[index.row()][4])
else:
return QVariant()
def setData(self,index,value,role=Qt.EditRole):
if not index.isValid():
return False
value=QVariant(value)
if role == Qt.EditRole:
self.listdata[index.row()][1]=value
elif role == Qt.UserRole:
self.listdata[index.row()][0]=value
elif role == Qt.ToolTipRole:
self.listdata[index.row()][2]=value
elif role == Qt.ForegroundRole:
self.listdata[index.row()][3]=value
elif role == 33: #isDeleted
self.listdata[index.row()][4]=value
else:
return False
self.dirty = True
self.emit(SIGNAL("dataChanged(QModelIndex,QModelIndex)"),index,index)
return True
def flags(self,index):
if not index.isValid():
return Qt.ItemIsEnabled
return Qt.ItemFlags(QAbstractListModel.flags(self,index)|Qt.ItemIsEditable)
def Print(self):
for i in self.listdata:
print '%i:%s (%s)'%(i[0].toInt()[0],i[1],i[2].toString())
class MyForm(QDialog):
def __init__(self,parent=None):
super(MyForm,self).__init__(parent)
self.resize(340, 180)
self.setWindowTitle("Editing ListView Items")
self.MyView=QListView(self)
self.MyView.setGeometry(QRect(20, 20, 300, 120))
self.MyView.setModel(MyModel(self))
self.buttonBox = QDialogButtonBox(self)
self.buttonBox.setGeometry(QRect(120, 140, 200, 32))
self.buttonBox.setOrientation(Qt.Horizontal)
self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel|QDialogButtonBox.Ok)
self.connect(self.buttonBox, SIGNAL("accepted()"), self.accept)
self.connect(self.buttonBox, SIGNAL("rejected()"), self.reject)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyForm()
window.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
我明白了。您必须实施QAbstractItemModel
,并且必须覆盖bool QAbstractItemModel.setData (self, QModelIndex index, QVariant value, int role = Qt.EditRole)。问题是你只是忘记事件Qt.EditRole
来显示数据。要修复它们,请在方法覆盖中添加此事件;
def data(self, index, role):
if not index.isValid():
return QVariant()
if role == Qt.DisplayRole:
return QVariant(self.listdata[index.row()][1])
elif role == Qt.UserRole:
return QVariant(self.listdata[index.row()][0])
elif role == Qt.ToolTipRole:
return QVariant(self.listdata[index.row()][2])
elif role == Qt.ForegroundRole: #Qt.ForegroundRole & BackgroundRole BUG for Combobox (pyqt4, ubuntu 10.04)
color=self.listdata[index.row()][3].toInt()[0]
if color>0:
if color==6:
return(QColor(Qt.lightGray))
elif color==7:
return(QColor(Qt.red))
elif color==8:
return(QColor(Qt.green))
elif role == 33: #isDeleted
return QVariant(self.listdata[index.row()][4])
elif role == Qt.EditRole: # <- Edit event
return QVariant(self.listdata[index.row()][1]) # <- Show current data
else:
return QVariant()