Results 1 to 8 of 8

Thread: QTreeView: Drag and Drop

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2009
    Posts
    23
    Thanks
    3

    Default QTreeView: Drag and Drop

    Hi all,

    I am currently developping a custom tree view and I would like to enable drag and drop,
    From the tree picture attached to this message, I would like to be able to reorder 1rst level element.

    For now, I was able to enable the DnD function but I am somewhat lost with my model implementation.
    From the Model/View programming tutorial, I should implement all those methods: QAbstractItemModel::insertRows(), QAbstractItemModel::removeRows(), QAbstractItemModel::dropMimeData() and QAbstractItemModel::setData().

    With the available examples and tutorial, I don't understand this mechanism.
    In my case, I do have a tree model which contains nodes. Those nodes contains Tree Item (== data). When I insert a child to an existing node, I have the following code:
    Qt Code:
    1. bool ResourceTreeNode::insertChildren(int position, int count, CommonTreeItem* treeItem)
    2. {
    3. if (position < 0 || position > m_childrens.size()) {
    4. return false;
    5. }
    6.  
    7. for (int row = 0; row < count; ++row) {
    8. ResourceTreeNode* nodePtr = new ResourceTreeNode(treeItem, this);
    9. m_childrens.insert(position, nodePtr);
    10. }
    11.  
    12. return true;
    13. }
    To copy to clipboard, switch view to plain text mode 

    From my understanding I should write a method QAbstractItemModel::insertRows(int row, int count, const QModelIndex &parent) in my custom model which call the method ResourceTreeNode::insertChildren.
    The problem is that I don't get how I can create my TreeItem (contained in my tree node) from those attributes: row, count and parent index? I guess I should get the datas from the node I am dropping to create the new one but I really don't see the trick?

    Does somebody have an example using a custom model (with more than one column)?

    Sometimes I even read people who implement drag and drop event. Should I do that also? and why?

    Thank you for helping me.

    Best Regards,
    Lionel
    Attached Images Attached Images

Similar Threads

  1. Preserving structure in QTreeView drag and drop
    By Tito Serenti in forum Qt Programming
    Replies: 1
    Last Post: 14th February 2009, 03:39
  2. Drag and drop in QTreeView
    By Valheru in forum Qt Programming
    Replies: 3
    Last Post: 27th July 2008, 09:36
  3. Drag and Drop on Whitespace of QTreeView
    By T1c4L in forum Qt Programming
    Replies: 6
    Last Post: 14th May 2008, 13:29
  4. Drag & drop items on the same QTreeView
    By wind in forum Qt Programming
    Replies: 2
    Last Post: 11th October 2006, 14:29
  5. Drag & drop for QTreeView
    By yogeshm02 in forum Qt Programming
    Replies: 2
    Last Post: 30th January 2006, 14:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.