用模型创建了一个类QFileSystemModel。问题是应该显示缩略图而不是图标(如在常规 Windows 文件管理器中)。
class MyFileManager(QListView):
def __init__(self):
super().__init__()
self.mdl = QFileSystemModel()
self.mdl.setRootPath(QDir.rootPath())
self.setModel(self.mdl)
self.setWindowTitle('менеджер файлов')
self.setRootIndex(self.mdl.index(QDir.currentPath()))
self.setAcceptDrops(True)
self.setDropIndicatorShown(True)
self.setDragEnabled(True)
self.setViewMode(QListView.IconMode)
self.setMovement(QListView.Free)
self.setGridSize(QSize(100, 100))
self.setIconSize(QSize(80, 80))
我试图创建一个额外的类,它的意思是将图标更改为缩影。但随后机会就消失了Drag & Drop。
class MyListModel(QAbstractListModel):
def __init__(self, datain, parent=None, *args):
""" datain: a list where each item is a row
"""
QAbstractListModel.__init__(self, parent, *args)
self.listdata = datain
def rowCount(self, parent=QModelIndex()):
return len(self.listdata)
def data(self, index, role):
if index.isValid() and role == Qt.DecorationRole:
return QIcon(QPixmap(self.listdata[index.row()]))
if index.isValid() and role == Qt.DisplayRole:
return QVariant(path.splitext(path.split(self.listdata[index.row()])[-1])[0])
else:
return QVariant()
可以做些什么来保存拖放并用缩略图替换图标?最好带模型QFileSystemModel。
是的,你建议的工作。问题是有损坏的图像。
决定了!需要使用自己的类 MyIconProvider(QFileIconProvider) 并设置 setIconProvider(MyIconProvider())
在我们的结构中,添加提供者分配行:
结果,我的工作代码如下所示: