QFileSystemModel 拖拽到其他QTreeView当中

这篇博客介绍了如何在Qt中实现QFileSystemModel中的文件路径信息拖拽到其他QTreeView的功能。通过重写QFileSystemModel和目标QTreeView的Model的相关方法,支持拖放操作,并在目标视图中添加新行。博客内容涉及Qt的拖放机制、QMimeData和Qt::DropActions等相关技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

研究之后发现这种拖拽在任意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 =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值