QT系列教程(21) 自定义模型的拖动实现

自定义模型的拖动

便捷类的拖动实现很简单,今天我们介绍自己定义的ListModel模型如何实现拖动。在之前的ListModel项目基础上,我们先对View视图实现拖动操作.

cpp 复制代码
 //设置选择模式为单选
    listView.setSelectionMode(QAbstractItemView::ExtendedSelection);
    //设置可拖拽
    listView.setDragEnabled(true);
    //设置可拖放
    listView.setAcceptDrops(true);
    //设置显示拖放位置
    listView.setDropIndicatorShown(true);

对模型实现拖动

在ListModel添加声明

cpp 复制代码
  //编写拖动逻辑
    virtual QStringList mimeTypes() const;
    virtual QMimeData *mimeData(const QModelIndexList &indexes) const;
    virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action,
                              int row, int column, const QModelIndex &parent);
    virtual Qt::DropActions supportedDropActions() const;

自定义一个类型,用来表示拖动导出的类型

cpp 复制代码
//拖放时导出的类型
QStringList StringListModel:: mimeTypes() const{
    QStringList types;
    //自定义类型
    types << "application/zack.list";
    return types;
}

将拖动的数据放入mimedata中

cpp 复制代码
QMimeData *StringListModel::mimeData(const QModelIndexList &indexes) const
{
    QMimeData * mimeData = new QMimeData();
    //字节数组
    QByteArray encodeData;
    QDataStream stream(&encodeData, QIODevice::WriteOnly);
    foreach(const QModelIndex& index, indexes){
        if(index.isValid()){
            QString text = data(index, Qt::DisplayRole).toString();
            stream << text;
        }
    }

    //将数据放入到QMimeData中
    mimeData->setData("application/zack.list", encodeData);
    return mimeData;

}

将拖放的数据从mimedata中导出

cpp 复制代码
bool StringListModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
    //如果放入的动作是ignoreaction
    if(action == Qt::IgnoreAction){
        return true;
    }
    //如果数据的格式不是指定的格式,那么返回false
    if(!data->hasFormat("application/zack.list")){
        return false;
    }

    //因为这里是列表, 只用一列, 所以列大于0时返回false
    if(column > 0){
        return false;
    }

    //设置开始插入行
    int beginRow;
    if(row != -1){
        beginRow = row;
    }else if(parent.isValid()){
        beginRow = parent.row();
    }else {
        beginRow = rowCount(QModelIndex());
    }

    //将数据从QMimeData 中读取出来, 然后插入到模型中
    QByteArray encodeData = data->data("application/zack.list");
    //stream流
    QDataStream stream(&encodeData, QIODevice::ReadOnly);
    //统计插入的数据
    QStringList newItems;
    //统计插入的行数
    int rows = 0;
    while(!stream.atEnd()){
        QString text;
        stream >> text;
        newItems << text;
        ++ rows;
    }
    //插入指定行数
    insertRows(beginRow, rows, QModelIndex());
    //批量修改行数数据
    foreach(const QString& text, newItems){
        QModelIndex idx = index(beginRow, 0, QModelIndex());
        setData(idx,text);
        beginRow++;
    }

    return true;
}

为了能让我们的item拖动,需要重新实现flags变量,使其支持拖放

cpp 复制代码
Qt::ItemFlags  StringListModel::flags(const QModelIndex& index) const{
    //索引无效可以接受放入操作
    if(!index.isValid())
         return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled;
    //索引有效,可以接受拖拽和放入操作
    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable
            | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
}

然后我们设置支持的拖放动作为移动和复制

cpp 复制代码
Qt::DropActions StringListModel::supportedDropActions() const
{
    //设置支持放入动作,允许copy和move
    return Qt::CopyAction | Qt::MoveAction;
}

拖动效果

源码链接

源码链接
https://gitee.com/secondtonone1/qt-learning-notes

相关推荐
北冥湖畔的燕雀3 小时前
C++泛型编程(函数模板以及类模板)
开发语言·c++
QX_hao4 小时前
【Go】--map和struct数据类型
开发语言·后端·golang
你好,我叫C小白4 小时前
C语言 循环结构(1)
c语言·开发语言·算法·while·do...while
Evand J6 小时前
【MATLAB例程】基于USBL和DVL的线性回归误差补偿,对USBL和DVL导航数据进行相互补偿,提高定位精度,附代码下载链接
开发语言·matlab·线性回归·水下定位·usbl·dvl
Larry_Yanan7 小时前
QML学习笔记(四十二)QML的MessageDialog
c++·笔记·qt·学习·ui
爱喝白开水a7 小时前
LangChain 基础系列之 Prompt 工程详解:从设计原理到实战模板_langchain prompt
开发语言·数据库·人工智能·python·langchain·prompt·知识图谱
Neverfadeaway7 小时前
【C语言】深入理解函数指针数组应用(4)
c语言·开发语言·算法·回调函数·转移表·c语言实现计算器
武子康7 小时前
Java-152 深入浅出 MongoDB 索引详解 从 MongoDB B-树 到 MySQL B+树 索引机制、数据结构与应用场景的全面对比分析
java·开发语言·数据库·sql·mongodb·性能优化·nosql
杰克尼7 小时前
JavaWeb_p165部门管理
java·开发语言·前端
一成码农7 小时前
JavaSE面向对象(下)
java·开发语言