Hi all,
I try to make DnD in QTableView. I made a sub-class of QAbstractTableModel.
Code:
#include "dragdroptableview.h" { } { listOfPairs = pairs; } { Q_UNUSED(parent); return listOfPairs.size(); } { Q_UNUSED(parent); return 2; } { if (!index.isValid()) if (index.row() >= listOfPairs.size() || index.row() < 0) if (role == Qt::DisplayRole) { QPair<QString, QString> pair = listOfPairs.at(index.row()); if (index.column() == 0) return pair.first; else if (index.column() == 1) return pair.second; } } { if (role != Qt::DisplayRole) if (orientation == Qt::Horizontal) { switch (section) { case 0: return tr("Name"); case 1: return tr("Address"); default: } } } { Q_UNUSED(index); for (int row=0; row < rows; row++) { QPair<QString, QString> pair(" ", " "); listOfPairs.insert(position, pair); } endInsertRows(); return true; } { Q_UNUSED(index); for (int row=0; row < rows; ++row) { listOfPairs.removeAt(position); } endRemoveRows(); return true; } { if (!index.isValid()) return Qt::ItemIsEnabled; } { QStringList types; types << "application/vnd.text.list"; return types; } { QByteArray encodedData; QModelIndex index; foreach (index, indexes) { if (index.isValid()) { QString text; text = data(index, Qt::DisplayRole).toString(); stream << text; } } mimeData->setData("application/vnd.text.list", encodedData); return mimeData; } Qt::DropAction DragDropTableView::supportedDropActions() { return Qt::MoveAction; } Qt::DropAction action, int row, int column, { Q_UNUSED(column) if (!data->hasFormat("application/vnd.text.list")) { return false; } if (action == Qt::IgnoreAction) { return true; } int endRow = 0; if (!parent.isValid() && row < 0) { endRow = listOfPairs.count(); } else if (!parent.isValid()) { endRow = qMin(row, listOfPairs.count()); } else { endRow = parent.row(); } while (!stream.atEnd()) { QString first, second; stream >> first >> second; listOfPairs.insert(endRow, pair); endInsertRows(); endRow ++; } return true; }
When I DnD some item from first table to second the item is copied into second table, but not removed from first table.
Any ideas?