研究之后发现这种拖拽在任意model当中都是通用的,这边实现了把QFileSystemModel 当中的文件路径信息拖拽到其他QTreeView当中,其他信息也是可以拖拽的。直接上代码,写的比较粗糙,遇到问题直接来问w
继承重写QFileSystemModel
Qt::DropActions supportedDropActions() const override;
QStringList mimeTypes() const override;
virtual QMimeData *mimeData(const QModelIndexList &indexes) const override;
Qt::DropActions FileSystemModel::supportedDropActions() const
{
return Qt::CopyAction;
}
QMimeData *FileSystemModel::mimeData(const QModelIndexList &indexes) const
{
if (indexes.count() <= 0)
{
return 0;
}
// 只取第一个
QModelIndex index = indexes[0];
QMimeData *data = QAbstractItemModel::mimeData(indexes);
QList<QUrl> url_list;
data->setUrls(url_list << filePath(index));
return data;
}
继承重写拖拽入的QTreeView的Model
bool removeRows(int row, int count, const QModelIndex &parent =