在QListView中编辑项清除该项

2024-04-27 05:13:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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_())

Tags: selfindexreturnifvaluedefqtrole
1条回答
网友
1楼 · 发布于 2024-04-27 05:13:44

我明白了。你必须实现QAbstractItemModel,你的重写{a1}很酷。问题是您的just-forget事件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()

相关问题 更多 >