
Originally Posted by
fullmetalcoder
A piece of code would be of a great help here (including supportedDropActions() and dropMimeData() and anything else you think may be relevant)...

Sure, here is the constructor:
{
// Only the Move action is possible
//model()->QAbstractItemModel::setSupportedDragActions(Qt::MoveAction);
}
GgdTreeWidget::GgdTreeWidget(QWidget * parent) : QTreeWidget(parent)
{
// Only the Move action is possible
//model()->QAbstractItemModel::setSupportedDragActions(Qt::MoveAction);
}
To copy to clipboard, switch view to plain text mode
The "setSupportedDragActions" is in commentary so I could test a CopyAction. Here is the dropMimeData() function.
const QMimeData *data, Qt
::DropAction action
) {
// PROBLEM: FUNCTION IS NOT CALLED FROM A MOVE ACTION !
// The destination item is *parent
// The item that is being dragged is in selectedItems()
QList<QTreeWidgetItem *> selectList;
selectList = this->selectedItems();
myDraggedItem = selectList.at(0);
////////// TEST
GgdMainWindow * myMainWindow = qobject_cast<GgdMainWindow*>(parentWidget());
myMainWindow->setTitleText("dragged=" + myDraggedItem->text(0));
//////////
if (parent) idx = indexFromItem(parent);
bool isAccepted = model()->QAbstractItemModel::dropMimeData(data, action , index, 0, idx);
return isAccepted;
}
bool GgdTreeWidget::dropMimeData(QTreeWidgetItem *parent, int index,
const QMimeData *data, Qt::DropAction action)
{
// PROBLEM: FUNCTION IS NOT CALLED FROM A MOVE ACTION !
// The destination item is *parent
// The item that is being dragged is in selectedItems()
QList<QTreeWidgetItem *> selectList;
QTreeWidgetItem * myDraggedItem;
selectList = this->selectedItems();
myDraggedItem = selectList.at(0);
////////// TEST
GgdMainWindow * myMainWindow = qobject_cast<GgdMainWindow*>(parentWidget());
myMainWindow->setTitleText("dragged=" + myDraggedItem->text(0));
//////////
QModelIndex idx;
if (parent) idx = indexFromItem(parent);
bool isAccepted = model()->QAbstractItemModel::dropMimeData(data, action , index, 0, idx);
return isAccepted;
}
To copy to clipboard, switch view to plain text mode
Here is the supportedActions()
Qt::DropActions GgdTreeWidget::supportedDropActions() const
{
// Only the Move action is possible
return Qt::MoveAction;
}
Qt::DropActions GgdTreeWidget::supportedDropActions() const
{
// Only the Move action is possible
return Qt::MoveAction;
}
To copy to clipboard, switch view to plain text mode
If I add "| Qt::CopyAction" and do a normal drag and drop (copy) the dropMimeData function gets called (I tested by placing a Breakpoint in it). If I hold shift and do a move action, it never steps into the dropMimeData function.
Is that the code you needed?
Here is the part where I create the TreeWidget from my MainWindow:
void GgdMainWindow::createTreeWidget()
{
// Creates and sets dimensions for the Tree Widget
mTree = new GgdTreeWidget(this);
mTree
->setGeometry
(QRect(10,
10,
171,
411));
// Sets drag and drop
mTree->setColumnCount(1);
mTree->setDragEnabled(true);
mTree->setAcceptDrops(true);
mTree->setDropIndicatorShown(true);
mTree->show();
}
void GgdMainWindow::createTreeWidget()
{
// Creates and sets dimensions for the Tree Widget
mTree = new GgdTreeWidget(this);
mTree->setGeometry(QRect(10, 10, 171, 411));
mTree->headerItem()->setText(0, QApplication::translate("GgdMainWindow", "Elements", 0, QApplication::UnicodeUTF8));
// Sets drag and drop
mTree->setColumnCount(1);
mTree->setSelectionMode(QAbstractItemView::SingleSelection);
mTree->setDragEnabled(true);
mTree->setAcceptDrops(true);
mTree->setDropIndicatorShown(true);
mTree->show();
}
To copy to clipboard, switch view to plain text mode
Bookmarks