我有一个QListView
来自自定义的显示数据ListModel
。在“常规”视图ListMode
模式IconMode
(
这是相关的代码。我省略了主窗口和任何其他杂物,但如果有帮助,我会包括它。
# Model
class TheModel(QtCore.QAbstractListModel):
def __init__(self, items = [], parent = None):
QtCore.QAbstractListModel.__init__(self, parent)
self.__items = items
def appendItem(self, item):
self.__items.append(item)
# item was added to end of list, so get that index
index = len(self.__items) - 1
# data was changed, so notify
self.dataChanged.emit(index, index)
def rowCount(self, parent):
return len(self.__items)
def data(self, index, role):
image = self.__items[index.row()]
if role == QtCore.Qt.DisplayRole:
# name
return image.name
if role == QtCore.Qt.DecorationRole:
# icon
return QtGui.QIcon(image.path)
return None
# ListView
class TheListView(QtGui.QListView):
def __init__(self, parent=None):
super(Ui_DragDropListView, self).__init__(parent)
self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
self.setIconSize(QtCore.QSize(48, 48))
self.setViewMode(QtGui.QListView.IconMode)
# ...