Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeView internal drag'n'drop

QTreeView internal drag'n'drop

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtreeviewdrag and drop
3 Posts 2 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Joel BodenmannJ Offline
    Joel BodenmannJ Offline
    Joel Bodenmann
    wrote on last edited by Joel Bodenmann
    #1

    I have custom subclasses of a QTreeView and a QAbstractItemModel. So far everything is working. What I would like to add is the option that the user can change the parent of an item in the tree view by dragging it around. I followed the corresponding documentation and extended my classes:

    MyTreeView:

    setSelectionMode(QAbstractItemView::SingleSelection);
    setDragEnabled(true);
    setAcceptDrops(true);
    setDropIndicatorShown(true);
    

    MyTreeModel:

    Qt::DropActions TaskWidgetTreeModel::supportedDropActions() const
    {
        return Qt::MoveAction;
    }
    
    Qt::ItemFlags TaskWidgetTreeModel::flags(const QModelIndex& index) const
    {
        if (!index.isValid()) {
            return 0;
        }
    
        // Default flags
        Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    
        // Add item specific flags
        if (index.isValid())
            return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | flags;
        else
            return Qt::ItemIsDropEnabled | flags;
    
        return flags;
    }
    

    After those modifications I am able to drag an item around in the tree using the mouse. However, the changes are not applied. This is not surprising to me. However, the documentation mentions that the QAbstractItemModel provides sane implementations for the required drag(), drop() and mineData() methods so that implementing those is usually not necessary. It also mentions that the model must support removeRow() and all that stuff which is implemented in my model and works well for everything that is not related to drag'n'drop.

    Can anybody tell me what else I need to do in order to be able to actually process the drag'n'droping?

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    1 Reply Last reply
    0
    • Joel BodenmannJ Offline
      Joel BodenmannJ Offline
      Joel Bodenmann
      wrote on last edited by
      #2

      Bump... anyone? :(

      Industrial process automation software: https://simulton.com
      Embedded Graphics & GUI library: https://ugfx.io

      P 1 Reply Last reply
      0
      • Joel BodenmannJ Joel Bodenmann

        Bump... anyone? :(

        P Offline
        P Offline
        pvt.peter
        wrote on last edited by
        #3

        Hi @Joel-Bodenmann
        Have you got any solution/feedback for your question?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved