There's a lot of discussion of DragNDrop with QTreeView widgets but I can't seem to find the "golden nugget" I need to get the basic functionality to work right. New entries get created in my tree but they have empty data. I believe it is a problem with my dropMimeData() method, but where?
I've startetd with the "editabletreemodel" example (see http://doc.trolltech.com/4.3/itemvie...treemodel.html) and made the following changes:
In the constructor for mainwindow I've added:
view->setDragEnabled(true);
view->setAcceptDrops(true);
view->setDropIndicatorShown(true);
view->setDragEnabled(true);
view->setDragDropMode(QAbstractItemView::InternalMove);
view->setAcceptDrops(true);
view->setDropIndicatorShown(true);
To copy to clipboard, switch view to plain text mode
In the TreeModel I've modified the flags() method to return:
Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
To copy to clipboard, switch view to plain text mode
and added these two methods to TreeModel:
Qt::DropActions TreeModel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
bool TreeModel
::dropMimeData(const QMimeData *data,
Qt::DropAction action,
int row, int column,
{
return true;
}
return false;
}
Qt::DropActions TreeModel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
bool TreeModel::dropMimeData(const QMimeData *data,
Qt::DropAction action,
int row, int column,
const QModelIndex &parent)
{
if (QAbstractItemModel::dropMimeData(data, action, row, column, parent)) {
return true;
}
return false;
}
To copy to clipboard, switch view to plain text mode
When running this code and doing a drag and drop new (empty) items are created in the appropriate destination. I'm just not getting the actual data elements copied over. Should I somehow be able to parse the "application/x-qabstractitemmodeldatalist" data myself? Is it necessary to implement TreeModel::mimeData() and create my own mime types?
What are other people doing for drag and drop with QTreeView?
Bookmarks