我基于 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