C++ (Qt) DraggedModel* m1 = new DraggedModel(QStringList() << "One" << "Two" << "Three"); ui->listView->setModel(m1); DraggedModel* m2 = new DraggedModel(); ui->listView_2->setModel(m2); ui->listView->setSelectionMode(QAbstractItemView::ExtendedSelection); ui->listView->setDragEnabled(true); ui->listView->setAcceptDrops(true); ui->listView->setDropIndicatorShown(true); ui->listView_2->setSelectionMode(QAbstractItemView::ExtendedSelection); ui->listView_2->setDragEnabled(true); ui->listView_2->setAcceptDrops(true); ui->listView_2->setDropIndicatorShown(true);
C++ (Qt)DraggedModel::DraggedModel(const QStringList &strings) :ss(strings){} int DraggedModel::rowCount(const QModelIndex &) const{ return ss.count();} QVariant DraggedModel::headerData(int section, Qt::Orientation orientation, int role) const{ if ((role == Qt::DisplayRole) && (orientation==Qt::Horizontal)) return "Burkhumm"; else return QAbstractItemModel::headerData(section, orientation, role);} QVariant DraggedModel::data(const QModelIndex &index, int role) const{ if (!index.isValid()) return QVariant(); if (index.row() >= ss.count()) return QVariant(); if (role==Qt::DisplayRole) return ss[index.row()]; else return QVariant();} Qt::DropActions DraggedModel::supportedDropActions() const{ return Qt::CopyAction | Qt::MoveAction;} Qt::ItemFlags DraggedModel::flags(const QModelIndex &index) const{ Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index); if (index.isValid()) return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags; else return Qt::ItemIsDropEnabled | defaultFlags;} QStringList DraggedModel::mimeTypes() const{ return QStringList() << "text/plain";} QMimeData *DraggedModel::mimeData(const QModelIndexList &indexes) const{ QMimeData *mimeData = new QMimeData(); if (!indexes.isEmpty()) mimeData->setText(ss[indexes[0].row()]); return mimeData;} bool DraggedModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent){ if (action == Qt::IgnoreAction) return true; if (!data->hasFormat("text/plain")) return false; if (column > 0) return false; beginResetModel(); ss << data->text(); endResetModel(); return true;}#include <iostream>bool DraggedModel::removeRows(int row, int count, const QModelIndex &parent){ std::cout << "DraggedModel::removeRows" << std:: endl; beginResetModel(); ss.removeAt(row); endResetModel(); return true;}