PDA

View Full Version : QTreeView Drag&Drop behaviour and how to disable drop indicator



flautzr
28th June 2019, 12:36
Hi,

after a lot of research and trial and error, i hit a dead end. What i want is a QTreeView that has drag&drop enabled, but only file elements inside should be dragable (and not accept drops) and only folder elements should accept drops, and not be dragable.

if i configure the QTreeView like this


ptrTreeView->setDragEnabled(true);
ptrTreeView->setAcceptDrops(true);

or like this


ptrTreeView->setDragDropMode(QAbstractItemView::DragDropMode::D ragDrop);

and set the items to


ptrFileItem->setDragEnabled(true);
ptrFileItem->setDropEnabled(false);

and the folders to


ptrFolderItem->setDragEnabled(false);
ptrFolderItem->setDropEnabled(true);

every item is still drag and dropable as if the the configuration of the tree overrides the individual configuration of the items. Do you guys know of a way where this is possible?

Another approach I tried is to let every item have both drag&drop ability but to modify the drop indicator and block/allow the actual drag&dropping in the drag&drop functions, but i did not succeed wit that either.
I created a custom model class derived from QStandardItemModel in which the flags fundtion is overridden


class MyModel: public QStandardItemModel
{
Q_OBJECT

public:
MyModel(QWidget *parent = Q_NULLPTR) : QStandardItemModel(parent) {};

virtual Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
}

In the flags function i analyzed the current Item and modified the Qt::ItemFlags so that Drag and Drop was enabled/disabled but still the drop indicator was shown while hovering over all the file/folder items. Is this approach wrong and if so, do you know of a working one?