0
아래의 코드는 QListView
을 .model()
으로 만듭니다. itemClicked
에서 self.setData(index, QtCore.QVariant(inst))
으로 이전에 저장된 값을 얻으려고 index.data()
에 액세스하려고합니다. 그러나 저장된 값 (예 : ['Bison','Panther','Elephant']
, ['Duck','Hawk','Pigeon']
)을 반환하는 대신 키 이름 (예 : 'Animals'
, 'Birds'
또는 'Fish'
)을 반환합니다. 의도 한대로 작동하도록 코드에서 변경해야하는 항목은 무엇입니까? .model()에서 데이터를 저장하고 가져 오는 방법
from PyQt4 import QtCore, QtGui
app=QtGui.QApplication(sys.argv)
class Model(QtCore.QAbstractListModel):
def __init__(self, *args):
QtCore.QAbstractListModel.__init__(self, *args)
self.items=[]
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.items)
def data(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.ItemDataRole:
return self.data(index)
elif role==QtCore.Qt.DisplayRole:
return QtCore.QVariant(self.items[index.row()])
def addItems(self, instDict):
for key in instDict:
inst=instDict.get(key)
index=QtCore.QModelIndex()
self.beginInsertRows(index, 0, 0)
self.setData(index, QtCore.QVariant(inst))
self.items.extend(instDict)
self.endInsertRows()
class ListView(QtGui.QListView):
def __init__(self):
super(ListView, self).__init__()
self.model= Model()
self.model.addItems({'Animals':['Bison','Panther','Elephant'],'Birds':['Duck','Hawk','Pigeon'],'Fish':['Shark','Salmon','Piranha']})
self.setModel(self.model)
self.clicked.connect(self.itemClicked)
self.show()
def itemClicked(self, index):
print 'self.data(index).toString(): %s'%index.data().toString()
print 'self.model.data(index).toString(): %s'%self.model.data(index).toString()
window=ListView()
sys.exit(app.exec_())
나중에 편집 : 여기
데이터 인수 중첩 사전 함께 완전히 동작 예이다.
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.modelDict)
def data(self, index, role):
if index.isValid():
if role==QtCore.Qt.ItemDataRole:
key=str(index.data().toString())
returnedValue=self.modelDict.get(key)
return QtCore.QVariant(returnedValue)
elif role==QtCore.Qt.DisplayRole:
return QtCore.QVariant(self.items[index.row()])
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()
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):
itemTitle=self.model.data(index, QtCore.Qt.DisplayRole).toString()
itemData=self.model.data(index, QtCore.Qt.ItemDataRole).toPyObject()
print 'itemTitle: "%s" itemData: %s'%(itemTitle,itemData)
window=ListView()
sys.exit(app.exec_())
작품! 감사! 당신은'index.data()'가'data()'메소드를 호출 할 때 어떤 값을 반환하는지 어떻게 알 수 있습니까?'data()'는'ItemDataRole' 또는'DisplayRole' 중에서 어떻게 선택합니까? 'index.data()'를 호출하는 동안 어떤 역할에 대해 반환 값을 지정하지 않았기 때문에'.data() '가 반환 할 것을 어떻게 알 수 있습니까? ... – alphanumeric