
Originally Posted by
onamatic
QUESTION: Where can I fix the QTreeView's default drop action? Is there some kind of compatibility switch at Application level?
To answer my own question (please jump in if I've missed something):
There doesn't appear to be any "compatibility" switch anywhere.
The erroneous default dropAction is caused by a mistake in the default implementation of QAbstractItemView::startDrag; override it to get the correct behaviour.
The example below does most of what the default implementation does but without access to protected members it doesn't emulate the fancy drag icon creation; you just get the standard drag box instead of the item's title. Thanks to another contributor to this forum (whose name I've lost) for most of the code.
{
qDebug() << "QAbstractItemView::startDrag; begin";
QModelIndexList indexes = selectedIndexes();
QList<QPersistentModelIndex> persistentIndexes;
if (indexes.count() > 0) {
QMimeData *data
= model
()->mimeData
(indexes
);
if (!data)
return;
for (int i = 0; i<indexes.count(); i++){
qDebug() << "\tDragged item to delete" << i << " is: \"" << idx.data(NODE_TITLE).toString() << "\"";
qDebug() << "Row is: " << idx.row();
}
QPixmap pixmap
= indexes.
first().
data(Qt
::DecorationRole).
value<QPixmap>
();
drag->setPixmap(pixmap);
drag->setMimeData(data);
drag
->setHotSpot
(QPoint(pixmap.
width()/2, pixmap.
height()/2));
Qt::DropAction defaultDropAction = Qt::IgnoreAction;
if (supportedActions
& Qt
::MoveAction && dragDropMode
() != QAbstractItemView::InternalMove) defaultDropAction = Qt::MoveAction; //was Qt::CopyAction THIS WAS THE CULPRIT!
if ( drag->exec(supportedActions, defaultDropAction) == Qt::MoveAction ){
//when we get here any copying done in dropMimeData has messed up our selected indexes
//that's why we use persistent indexes
for (int i = 0; i<indexes.count(); i++){
qDebug() << "\tDragged item to delete" << i << " is: " << idx.data(NODE_TITLE).toString();
qDebug() << "Row is: " << idx.row();
if (idx.isValid()){ //the item is not top level
model()->removeRow(idx.row(), idx.parent());
}
else{
}
}
}
}
}
void QAbstractItemView::startDrag(Qt::DropActions supportedActions)
{
qDebug() << "QAbstractItemView::startDrag; begin";
QModelIndexList indexes = selectedIndexes();
QList<QPersistentModelIndex> persistentIndexes;
if (indexes.count() > 0) {
QMimeData *data = model()->mimeData(indexes);
if (!data)
return;
for (int i = 0; i<indexes.count(); i++){
QModelIndex idx = indexes.at(i);
qDebug() << "\tDragged item to delete" << i << " is: \"" << idx.data(NODE_TITLE).toString() << "\"";
qDebug() << "Row is: " << idx.row();
persistentIndexes.append(QPersistentModelIndex(idx));
}
QPixmap pixmap = indexes.first().data(Qt::DecorationRole).value<QPixmap>();
QDrag *drag = new QDrag(this);
drag->setPixmap(pixmap);
drag->setMimeData(data);
drag->setHotSpot(QPoint(pixmap.width()/2, pixmap.height()/2));
Qt::DropAction defaultDropAction = Qt::IgnoreAction;
if (supportedActions & Qt::MoveAction && dragDropMode() != QAbstractItemView::InternalMove)
defaultDropAction = Qt::MoveAction; //was Qt::CopyAction THIS WAS THE CULPRIT!
if ( drag->exec(supportedActions, defaultDropAction) == Qt::MoveAction ){
//when we get here any copying done in dropMimeData has messed up our selected indexes
//that's why we use persistent indexes
for (int i = 0; i<indexes.count(); i++){
QPersistentModelIndex idx = persistentIndexes.at(i);
qDebug() << "\tDragged item to delete" << i << " is: " << idx.data(NODE_TITLE).toString();
qDebug() << "Row is: " << idx.row();
if (idx.isValid()){ //the item is not top level
model()->removeRow(idx.row(), idx.parent());
}
else{
model()->removeRow(idx.row(), QModelIndex());
}
}
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks