Results 1 to 2 of 2

Thread: Basic QTreeView DragNDrop Question

  1. #1
    Join Date
    Oct 2009
    Thanked 3 Times in 2 Posts
    Qt products
    MacOS X Unix/X11 Windows

    Default Basic QTreeView DragNDrop Question

    There's a lot of discussion of DragNDrop with QTreeView widgets but I can't seem to find the "golden nugget" I need to get the basic functionality to work right. New entries get created in my tree but they have empty data. I believe it is a problem with my dropMimeData() method, but where?

    I've startetd with the "editabletreemodel" example (see and made the following changes:

    In the constructor for mainwindow I've added:
    Qt Code:
    1. view->setDragEnabled(true);
    2. view->setDragDropMode(QAbstractItemView::InternalMove);
    3. view->setAcceptDrops(true);
    4. view->setDropIndicatorShown(true);
    To copy to clipboard, switch view to plain text mode 

    In the TreeModel I've modified the flags() method to return:
    Qt Code:
    1. Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
    To copy to clipboard, switch view to plain text mode 

    and added these two methods to TreeModel:
    Qt Code:
    1. Qt::DropActions TreeModel::supportedDropActions() const
    2. {
    3. return Qt::CopyAction | Qt::MoveAction;
    4. }
    6. bool TreeModel::dropMimeData(const QMimeData *data,
    7. Qt::DropAction action,
    8. int row, int column,
    9. const QModelIndex &parent)
    10. {
    11. if (QAbstractItemModel::dropMimeData(data, action, row, column, parent)) {
    12. return true;
    13. }
    14. return false;
    15. }
    To copy to clipboard, switch view to plain text mode 

    When running this code and doing a drag and drop new (empty) items are created in the appropriate destination. I'm just not getting the actual data elements copied over. Should I somehow be able to parse the "application/x-qabstractitemmodeldatalist" data myself? Is it necessary to implement TreeModel::mimeData() and create my own mime types?

    What are other people doing for drag and drop with QTreeView?

  2. #2
    Join Date
    Jan 2006
    Thanked 52 Times in 52 Posts
    MacOS X Unix/X11

    Default Re: Basic QTreeView DragNDrop Question

    Yes, it is necessary to implement mimeData() yourself. See the itemview drag & drop documentation. There is a list of things you have to do:
    It's nice to be important but it's more important to be nice.

Similar Threads

  1. Question on checkable QStandardItem in QTreeView
    By ttvo in forum Qt Programming
    Replies: 1
    Last Post: 3rd April 2009, 00:30
  2. Replies: 2
    Last Post: 21st February 2008, 22:35
  3. QTreeView and QDirModel Header Sort Question
    By jimroos in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2007, 08:04
  4. Replies: 1
    Last Post: 15th March 2007, 20:45
  5. Basic question on new and delete
    By jcr in forum General Programming
    Replies: 25
    Last Post: 14th February 2006, 15:09

Tags for this Thread


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.