Refuse from removing row in Model after unsuccessful drop
Hello!
Win7, Qt 4.8.
I use Model/View based on QStandardItemModel and QTreeView. Some items are not allowed to get dropped items. When I try to drop into them, they don't get the dropped item (as they should), but the source item is removed after the failed drop attempt.
The same situation is described here. Is it really a bug?
I tried to reimplement dropMimeData, f.e:
Code:
bool TMyModel
::dropMimeData(const QMimeData * apData, Qt
::DropAction aAction,
int aRow,
int aColumn,
const QModelIndex & aParent
) {
if (/*some condition*/)
return QAbstractItemModel::dropMimeData ( apData, Qt
::IgnoreAction, aRow, aColumn, aParent
);
}
or so
Code:
bool TMyModel
::dropMimeData(const QMimeData * apData, Qt
::DropAction aAction,
int aRow,
int aColumn,
const QModelIndex & aParent
) {
if (/*some condition*/)
return false;
}
RemoveRows is called in all cases and the dragged item disappears. Could I avoid the calling of removeRows?
Thanks for any help.
Re: Refuse from removing row in Model after unsuccessful drop
Can you show the implementation of your Dragging operation? (at the source object)
Re: Refuse from removing row in Model after unsuccessful drop
Thanks for reply.
Maybe the problem is here and I should reimplement some other functions? F.e., supportedDragActions? I try to reimplement only dropMimeData. (Also 'data' and 'setData' but I think they are not important here)
Re: Refuse from removing row in Model after unsuccessful drop
What to do with the dragged data on the source side has to be implemented on the soruce side - where the drag begins, and where the drop result returns.
Re: Refuse from removing row in Model after unsuccessful drop
It becomes a little bit clearer.
Really 'removeRows' is called in QAbstractItemView::startDrag in such a way:
Code:
if (drag->exec(supportedActions, defaultDropAction) == Qt::MoveAction)
d->clearOrRemove();
But I can't understand how to get out of it. Should I reimplement 'startDrag' at all or is it possible to avoid calling 'clearOrRemove' by some other tricks?
Re: Refuse from removing row in Model after unsuccessful drop
I think you are over complicating all of this.
Simply call ignore on your dragEnter event when the mime type of your drag should be ignored.
Re: Refuse from removing row in Model after unsuccessful drop
There are no certain mime types to ignore. The necessity to ignore drop depends on the drop position. F.e., I don't want to drop items in the second column in the tree.
Well, I did the following:
Code:
public:
void setErrorFlag(bool);
bool getErrorFlag();
private:
bool ErrorFlag;
Code:
bool TMyModel
::dropMimeData(const QMimeData * pData, Qt
::DropAction Action,
int Row,
int Column,
const QModelIndex & Parent
) {
if (/*some_error_condition*/)
{
setErrorFlag(true);
return false;
}
setErrorFlag(false);
Code:
void TMyTreeView
::dropEvent ( QDropEvent * pEvent
) {
// this cancels removeRows
TMyModel *pModel = dynamic_cast<TMyModel *>(model());
if (pModel != NULL)
{
if (pModel->getErrorFlag())
{
pEvent->setDropAction(Qt::IgnoreAction);
}
}
}
It works. But such a decision seems to be unobvious...