我已经基于QAbstractListModel创建了自己的自定义QListModel。问题是我输入后不确定如何查询模型中的数据。我假设我需要一种某种类型的getter函数,但我不确定如何实现它。
class CustomListModel(QtCore.QAbstractListModel):
def __init__(self, list = [], parent = None):
QtCore.QAbstractListModel.__init__(self, parent)
self.__list = list
def rowCount(self, parent):
return len(self.__list)
def data(self, index, role):
row = index.row()
value = self.__list[row]
if role == QtCore.Qt.ToolTipRole:
return 'test1: ' + value[0] + ' test2: ' + value[1]
if role == QtCore.Qt.DecorationRole:
pixmap = QtGui.QPixmap(images + 'small2.png')
icon = QtGui.QIcon(pixmap)
return icon
if role == QtCore.Qt.DisplayRole:
return 'test1: ' + value[0] + ' test2: ' + value[1]
def flags(self, index):
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def setData(self, index, value, role = QtCore.Qt.EditRole):
if role == QtCore.Qt.EditRole:
row = index.row()
def insertRows(self, position, rows, newList, parent=QtCore.QModelIndex()):
self.beginInsertRows(parent, position, position + rows - 1)
for i in range(rows):
self.__list.insert(position, newList)
self.endInsertRows()
return True
def removeRows(self, position, rows, parent=QtCore.QModelIndex()):
self.beginRemoveRows(parent, position, position + rows - 1)
for i in range(rows):
value = self.__list[position]
self.__list.remove(value)
self.endRemoveRows()
return True
答案 0 :(得分:0)
我不需要一个getter函数,我只需要执行以下操作。
data = self.list.index(0).data(role = QtCore.Qt.DisplayRole).toPyObject()
self.list是CustomListModel的一个实例。
我遇到的问题是返回的对象是Qt对象而不是Python对象,所以我无法提取数据。