在QListView中编辑项会清空该项
我对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_())
1 个回答
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()