gpt4 book ai didi

python - 如何使用 model() 删除项目后清理困惑

转载 作者:太空宇宙 更新时间:2023-11-03 18:11:25 25 4
gpt4 key购买 nike

下面的代码创建一个带有模型的 QListView。单击其项目会将其从模型中删除。但 QListView 仍然报告模型的项目数量保持不变(就像没有删除项目一样)。有什么办法可以解决吗?

enter image description here

class Model(QtCore.QAbstractListModel):
def __init__(self):
QtCore.QAbstractListModel.__init__(self)
self.items=[]
self.modelDict={}

def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.modelDict)

def data(self, index, role):
if not index.isValid():
return QtCore.QVariant()
if not (0 <= index.row() < len(self.items)):
return QtCore.QVariant()

if not index.isValid():
return QtCore.QVariant()
if role==QtCore.Qt.ItemDataRole:
key=str(index.data().toString())
returnedValue=self.modelDict.get(key)
return QtCore.QVariant(returnedValue)

elif role==QtCore.Qt.DisplayRole:
row=index.row()
itemTitle=self.items[row]
return QtCore.QVariant(itemTitle)

def addItems(self):
for key in self.modelDict:
index=QtCore.QModelIndex()
self.beginInsertRows(index, 0, 0)
self.items.append(key)

inst=self.modelDict.get(key)
self.setData(index, QtCore.QVariant(inst), QtCore.Qt.DisplayRole)

self.endInsertRows()

def removeByIndex(self, index):
if not index.isValid(): return
row=index.row()
self.beginRemoveRows(QtCore.QModelIndex(), row, 0)
self.items=self.items[:row]+self.items[row+1:]
self.endRemoveRows()

class ListView(QtGui.QListView):
def __init__(self):
super(ListView, self).__init__()
self.model= Model()
self.model.modelDict=elements
self.model.addItems()
self.setModel(self.model)
self.clicked.connect(self.itemClicked)
self.show()

def itemClicked(self, index):
print 'NUMBER OF ITEMS BEFORE DELETE: %s'%self.model.rowCount()
self.model.removeByIndex(index)
print 'NUMBER OF ITEMS AFTER DELETE: %s'%self.model.rowCount()

window=ListView()
sys.exit(app.exec_())

最佳答案

有关如何从模型中删除项目的完整示例。

import os,sys
from PyQt4 import QtCore, QtGui
app=QtGui.QApplication(sys.argv)
elements={'Animals':{1:'Bison',2:'Panther',3:'Elephant'},'Birds':{1:'Duck',2:'Hawk',3:'Pigeon'},'Fish':{1:'Shark',2:'Salmon',3:'Piranha'}}

class Model(QtCore.QAbstractListModel):
def __init__(self):
QtCore.QAbstractListModel.__init__(self)
self.items=[]
self.modelDict={}

def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.items)

def data(self, index, role):
if not index.isValid():
return QtCore.QVariant()
if not (0 <= index.row() < len(self.items)):
return QtCore.QVariant()

if not index.isValid():
return QtCore.QVariant()
if role==QtCore.Qt.ItemDataRole:
key=str(index.data().toString())
returnedValue=self.modelDict.get(key)
return QtCore.QVariant(returnedValue)

elif role==QtCore.Qt.DisplayRole:
row=index.row()
itemTitle=self.items[row]
return QtCore.QVariant(itemTitle)

def addItems(self):
for key in self.modelDict:
index=QtCore.QModelIndex()
self.beginInsertRows(index, 0, 0)
self.items.append(key)

inst=self.modelDict.get(key)
self.setData(index, QtCore.QVariant(inst), QtCore.Qt.DisplayRole)

self.endInsertRows()

def removeByIndex(self, index):
if not index.isValid(): return
row=index.row()
self.beginRemoveRows(QtCore.QModelIndex(), row, 0)
self.items=self.items[:row]+self.items[row+1:]
key=str(index.data().toString())
if self.modelDict.get(key): self.modelDict.pop(key,None)
self.endRemoveRows()

class ListView(QtGui.QListView):
def __init__(self):
super(ListView, self).__init__()
self.model= Model()
self.model.modelDict=elements
self.model.addItems()
self.setModel(self.model)
self.clicked.connect(self.itemClicked)
self.show()

def itemClicked(self, index):
print 'NUMBER OF ITEMS BEFORE DELETE: %s'%self.model.rowCount()
self.model.removeByIndex(index)
print 'NUMBER OF ITEMS AFTER DELETE: %s'%self.model.rowCount()

window=ListView()
sys.exit(app.exec_())

关于python - 如何使用 model() 删除项目后清理困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25859150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com