2
votes

I have a QTreeWidget and I enabled the drag and drop. Despite I would like the users to be able to drag and drop items inside the tree I don't want them to drop any dragged item on the top level. How can I do that ?

Let's say that I have predefined categories which are the top level items and I don't want to extend that list. But in the lower levels the user can create any number of items and also she/he can move those items around.

1
Without checking I'd guess you need at least to inherit from QTreeView. I've never tried it. Try filtering selectedIndexes() and react to the normal drag events (doc.qt.io/qt-5/examples-draganddrop.html)Ronny Brendel
Try to use QTreeWidget::invisibleRootItem to get root item and disable Qt::ItemIsDropEnabled flag for it. If this doesn't work, reimplement tree widget's dropMimeData and block dropping if parent is null or invisible root item.Pavel Strakhov
It will be much easier to use QStandardItemModel + QTreeView for such requests.Dmitry Sazonov

1 Answers

4
votes

Thanks for the help. It worked.

MyTreeWidget::MyTreeWidget( QWidget* aParent /*= nullptr*/ )
: QTreeWidget( aParent )
{
// ...
    auto rooItem = invisibleRootItem();
    rooItem->setFlags( rooItem->flags() ^ Qt::ItemIsDropEnabled );
}