Results 1 to 4 of 4

Thread: Drag and Dropping items in QTreeView

  1. #1
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Angry Drag and Dropping items in QTreeView

    Hello everyone,

    I am relatively new to Qt programming however I am pretty skilled in C++ programming and so I was hired on as a C++ GUI Programmer for a company. I have ran into a little bit of an issue that I can't seem to figure out. I have a built a QTreeView view that just shows the data in a selected hierarchical fashion. I am trying to enable the user to be able to drag and drop items from one place to another. I have a MainWindow class that inherits from QMainWindow and I am trying to get the MainWindow to catch the dropAction when moving items from one to place to another on the QTreeView (I will call view from now on). In doing so I have set the MainWindow in the constructor to accept drops and then disabled the view from excepting drops. As far as this is concerned it appears to be doing what I want. I then reimplemented the dragEnterEvent so that the MainWindow will know when something is being dragged and again that works as I would expect. Finally, I reimplemented the dropEvent and I am trying to get the correct index of the item that the drop event is trying to drop to so that I can call a function that I have already made that will correctly place the item where it needs to go. I am unable to get the correct index from the QTreeView and I am not sure how to proceed.

    Visual example of what I am trying to do:

    Before Move: After Move G to Directly underneath A:
    - A - A
    - B - G
    - C - B
    - D - C
    - E - D
    - F - E
    - G - F
    - H - H

    If this isn't formatting the way I would like Ill explain the above:

    A is the parent index of B,C, and D. D is the parent index of E and F. G and H only have the default parent index. I am trying to move G from default parent index to have A as its parent index when dropping. However I am unable to get the correct index of A, in this situation index (0,0) with parent item being default parent index (QModelIndex()), to be found by dropEvent.
    I have a function that already does exactly what I want but it's dependent on the QModelIndex of the view.

    My question is how do I get the dropEvent to give me the correct QModelIndex of the QTreeView to place G in the correct location?

    Here is the relevant code:

    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. //...
    4.  
    5. setAcceptDrops(true);
    6.  
    7. //...
    8. }
    9.  
    10. bool MainWindow::loadFile(const QString &fileName)
    11. {
    12. //...
    13. view->reset();
    14. view->setDragDropMode(QAbstractItemView::DragOnly);
    15. view->setDropIndicatorShown(true);
    16. view->setAcceptDrops(false);
    17. view->setModel(urnTree);
    18. view->setWindowTitle(tr("Urn Description"));
    19.  
    20. //...
    21. }
    22.  
    23. void MainWindow::dragEnterEvent(QDragEnterEvent *event)
    24. {
    25. event->acceptProposedAction();
    26. }
    27.  
    28. void MainWindow::dropEvent(QDropEvent *event)
    29. {
    30. cout << "Drop Event has occured!\n";
    31. QPoint curPos = event->
    32. QModelIndex curIndex = view->indexAt(curPos);
    33. curIndex = view->model()->index(curIndex.row(), curIndex.column(), curIndex.parent());
    34. TreeItem *curItem = urnTree->getItem(curIndex);
    35. cout << "Current index is: " << curIndex.row() << "," << curIndex.column() << endl;
    36. cout << "Current item on drop is: " << curItem->data(0).toString().toStdString() << endl;
    37. }
    To copy to clipboard, switch view to plain text mode 


    As you can see I am lost in how to get the correct QModelIndex from dropEvent and I am running out of ideas and appreciate any help. Please feel free to ask for more code if necessary.

    Thanks!!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and Dropping items in QTreeView

    If the event propagates from the view to the parent widget, it's coordinates are mapped to the coordinates of the parent widget whereas if you call any of the view's methods (like indexAt) it expects the coordinates to be in either the view's or the view's viewport (depending on the method) coordinate space. So you first have to map those coordinates using the family of mapTo*/mapFrom* methods.

    And with all do respect to your C++ skills think if exposing the internal representation of the tree and handling internal affairs of the tree in some external place (like the MainWindow class) is a good design decision of a skilled C++ developer.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Exclamation Re: Drag and Dropping items in QTreeView


    And with all do respect to your C++ skills think if exposing the internal representation of the tree and handling internal affairs of the tree in some external place (like the MainWindow class) is a good design decision of a skilled C++ developer.
    I am very new to Qt Programming and this is my first professional experience with Qt GUI programming (First programming job period to be exact). I work for a very small company so I pretty much have had to learn Qt on the fly and so that is why the code is pretty rough and my understanding of Qt GUI programming isn't the greatest. What would be a better design approach? Thanks for any help and keep the criticism as it is the only way I will get better.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and Dropping items in QTreeView

    This was a C++ related comment not a Qt related one. I would definitely try to handle the drag&drop mechanism in the view object. And would definitely get rid of the getItem() method in the tree.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 2
    Last Post: 20th August 2010, 05:18
  2. Replies: 1
    Last Post: 27th February 2010, 11:39
  3. Catch drag and drop items
    By nina1983 in forum Qt Programming
    Replies: 1
    Last Post: 23rd July 2008, 17:24
  4. drag/dropping outside app
    By musikit in forum Qt Programming
    Replies: 4
    Last Post: 29th February 2008, 17:34
  5. Drag & drop items on the same QTreeView
    By wind in forum Qt Programming
    Replies: 2
    Last Post: 11th October 2006, 14:29

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.