温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c++ - QTableView + QAbstractTableModel: Move rows via drag'n'drop
c++ drag-and-drop qabstracttablemodel QT qtableview

c++ - QTableView + QAbstractTableModel:通过拖放移动行

发布于 2020-12-19 12:14:16

我有一个QAbstractTableModel基于简单的模型QTableView

我的目标也很简单:允许通过拖放来移动/重新排序行。笔记:

  • 内部的变化QTableView应该反映在我的模型中;
  • 应该是内部的-移动只能在我的视图内执行,而不能导出外部MIME;
  • 我想拖放行。请勿拖放单独的项目
  • 拖动水平标题对我来说不是一个合适的解决方案,因为我希望隐藏标题并且因为我想让用户在任意位置抓住行来拖动它;

我已经接近目标了。但是,它仍然无法按我预期的那样工作。现在,我可以拖动行,但似乎任何单元格都可以接受删除,尽管我Qt::ItemIsDropEnabled只为全局表的父级指定了该字段,并且不为实际表项指定此标志,因为我不想删除它们,我想以某种方式放在“行之间”,只是执行行移动。因为表项出于某种原因可以接受掉落,所以我会产生奇怪的行为:如果掉落到任何行的第一个单元格,我都能实现我想要的结果:我的行正确移动。但是,如果我下降到任何行的非第一个单元格,那将完全出错。但是最好显示一下这里发生的情况的图片:

enter image description here

我的代码(正是我的问题的最小样本):

main.cpp

void setupView(QTableView &t)
{
    t.verticalHeader()->hide();
    t.horizontalHeader()->hide();
    t.horizontalHeader()->setStretchLastSection(true);

    t.setSelectionBehavior(QAbstractItemView::SelectRows);
    t.setSelectionMode(QAbstractItemView::SingleSelection);

    t.setDragEnabled(true);
    t.setDropIndicatorShown(true);
    t.setAcceptDrops(true);
    t.viewport()->setAcceptDrops(true);
    t.setDefaultDropAction(Qt::MoveAction);
    t.setDragDropMode(QTableView::InternalMove);
    t.setDragDropOverwriteMode(false);
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMainWindow w;

    QTableView *table = new QTableView(&w);
    setupView(*table);
    table->setModel(new TableModel);

    w.setCentralWidget(table);
    w.show();

    return a.exec();
}

tablemodel.cpp

#include "tablemodel.h"

TableModel::TableModel()
{
    // m_data is a QList<QStringList>
    m_data = {
        {"Name", "Kelly"},
        {"Age", "19"},
        {"Gender", "Female"},
    };
}

int TableModel::rowCount(const QModelIndex &parent) const {
    return m_data.size();
}

int TableModel::columnCount(const QModelIndex &parent) const {
    return 2;
}

QVariant TableModel::data(const QModelIndex &i, int r) const
{
    return (r == Qt::DisplayRole) ? m_data[i.row()][i.column()] : QVariant();
}

QVariant TableModel::headerData(int section, Qt::Orientation orientation, int r) const
{
    return QVariant();
}

Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable
                    | Qt::ItemIsEditable | Qt::ItemIsDragEnabled;

    if(!index.isValid()) {
        f |= Qt::ItemIsDropEnabled;
    }

    return f;
}

Qt::DropActions TableModel::supportedDropActions() const
{
    return Qt::MoveAction | Qt::CopyAction;
}

bool TableModel::setData(const QModelIndex &i, const QVariant &v, int r)
{
    if(r == Qt::EditRole || r == Qt::DisplayRole) {
        m_data[i.row()][i.column()] = v.toString();
        return true;
    }
    return false;
}

bool TableModel::setItemData(const QModelIndex &i, const QMap<int, QVariant> &roles)
{
    if(!roles.contains(Qt::EditRole) && !roles.contains(Qt::DisplayRole)) {
        return false;
    }

    m_data[i.row()][i.column()] = roles[Qt::DisplayRole].toString();
    return true;
}

bool TableModel::insertRows(int row, int count, const QModelIndex &parent)
{
    beginInsertRows(QModelIndex(), row, row + count - 1);
    for(int i = 0; i<count; ++i) {
        m_data.insert(row, QStringList({"", ""}));
    }
    endInsertRows();
    return true;
}

bool TableModel::removeRows(int row, int count, const QModelIndex &parent)
{
    beginRemoveRows(QModelIndex(), row, row + count - 1);
    for(int i = 0; i<count; ++i) {
        m_data.removeAt(row);
    }
    endRemoveRows();
    return true;
}

bool TableModel::moveRows(const QModelIndex &srcParent, int srcRow, int count,
                          const QModelIndex &dstParent, int dstChild)
{
    beginMoveRows(QModelIndex(), srcRow, srcRow + count - 1, QModelIndex(), dstChild);
    for(int i = 0; i<count; ++i) {
        m_data.insert(dstChild + i, m_data[srcRow]);
        int removeIndex = dstChild > srcRow ? srcRow : srcRow+1;
        m_data.removeAt(removeIndex);
    }
    endMoveRows();
    return true;
}

Please, give me some hint, what is wrong with model or view setup now.

UPD

For those who is interested in the solution:

bool TableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
    Q_UNUSED(parent);
    Q_UNUSED(column);

    if(row == -1) {
        row = rowCount();
    }

    return QAbstractTableModel::dropMimeData(data, action, row, 0, parent);
}

查看更多

提问者
Nikolai Shalakin
被浏览
0
Dmitry 2017-07-13 19:48

You should add dropMimeData method to your model and implement it properly. If the drop on the first column works fine for you, you could probably simply call QAbstractItemModel::dropMimeData from inside your model's dropMimeData with column parameter equal to 0 regardless of which column the drop was actually made on.