Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTableView + QAbstractTableModel: Move rows via drag'n'drop

I have a simple QAbstractTableModel-based model and a QTableView for it.

My aim is simple as well: allow to move/reorder rows via drag'n'drop. Notes:

  • D'n'd changes inside QTableView should be reflected in my model;
  • D'n'd supposed to be internal - movement should be performed only inside my view, no external MIME exports;
  • I want to drag and drop whole row. Separate items should not be dragged or dropped;
  • Dragging horizontal header is not a suitable solution for me because I want headers to be hided and because I want to let user to grab row at any place to drag it;

I'm reeeally close to my aim. But still it doesn't work as I expect. Now I can drag rows, but seems that any cell can accept a drop, although I've specified Qt::ItemIsDropEnabled only for a global table's parent and do not specify this flag for actual table items because I do not want to drop to them, I want somehow to drop "between the rows", just to perform row movement. Because table items for some reason can accept drops I get curious behaviour: if a drop to first cell of any row, I achieve exactly what I want: my row moves correctly. But if I drop to nonfirst cell of any row, it goes totally wrong. But it's better to show a pic of what happens here:

enter image description here

My code (minimal sample that has exactly my problem):

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);
}
like image 898
Nikolai Shalakin Avatar asked Jul 13 '17 10:07

Nikolai Shalakin


2 Answers

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.

like image 197
Dmitry Avatar answered Oct 15 '22 08:10

Dmitry


Nitpick: These two lines are not necessary:

t.setAcceptDrops(true);
t.viewport()->setAcceptDrops(true);
like image 27
Kuba hasn't forgotten Monica Avatar answered Oct 15 '22 08:10

Kuba hasn't forgotten Monica