Results 1 to 3 of 3

Thread: Drag & drop for QTreeView

  1. #1
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Drag & drop for QTreeView

    Hi

    I'm having dificulty using drag & drop for QTreeView. Actually i'm trying to move an item by drag n drop. I think i've setup everything as documented in qt4 docs. See the code below, may be you could find mistakes.

    So far i've noticed following misbehaviour: -
    • When an item is moved, a new copy of the item is created.
    • When i start dragging an item from say first column and drap at say second column, a new copy of the item is created with text of column1 in column2, column 2 in column3, so on....


    Set up QTreeView like this: -
    Qt Code:
    1. m_pDetailView->setDragEnabled(TRUE);
    2. m_pDetailView->setAcceptDrops(TRUE);
    3. m_pDetailView->setDropIndicatorShown(TRUE);
    To copy to clipboard, switch view to plain text mode 

    Have following in QAbstractItemMode derived class
    Qt Code:
    1. Qt::ItemFlags NowPlayingModel::flags(const QModelIndex& index)const{
    2. if(!index.isValid())
    3. return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled;
    4. Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled;
    5. if(mColumns.at(index.column()).isEditable)
    6. flags |= Qt::ItemIsEditable;
    7. return flags;
    8. }
    9.  
    10. Qt::DropActions NowPlayingModel::supportedDropActions() const{
    11. return Qt::CopyAction | Qt::MoveAction;
    12. }
    13. //This function is never called
    14. bool NowPlayingModel::removeRows(int row, int count, const QModelIndex &parent){
    15. qDebug("Remove");
    16. if(parent.isValid())
    17. return FALSE;
    18. beginRemoveRows(parent, row, row + count - 1);
    19. for(int i = 0; i < count; ++i){
    20. delete m_pAllData->takeAt(row);
    21. }
    22. endRemoveRows();
    23. return TRUE;
    24. }
    To copy to clipboard, switch view to plain text mode 

    Please guide me through the correct way. I've tried everything i could.

    Thanks in advance.

  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 & drop for QTreeView

    First of all make sure that you have "SelectRows" chosen in "Select Behavior" of the tree view. This will hopefully solve the problem with first and second column.

    As for the rest:
    Qt Code:
    1. Qt::DropActions NowPlayingModel::supportedDropActions() const{
    2. return Qt::CopyAction | Qt::MoveAction;
    3. }
    To copy to clipboard, switch view to plain text mode 

    This implies that you accept a copy action. If you don't want the item to be copied, make sure you only accept moving items.

    You might also want to reimplement the tree view's dropEvent to check wheather the item was dropped on the same widget it was picked from.

  3. #3
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag & drop for QTreeView

    Quote Originally Posted by wysota
    First of all make sure that you have "SelectRows" chosen in "Select Behavior" of the tree view. This will hopefully solve the problem with first and second column.
    Hey, it works
    Quote Originally Posted by wysota
    As for the rest:
    Qt Code:
    1. Qt::DropActions NowPlayingModel::supportedDropActions() const{
    2. return Qt::CopyAction | Qt::MoveAction;
    3. }
    To copy to clipboard, switch view to plain text mode 

    This implies that you accept a copy action. If you don't want the item to be copied, make sure you only accept moving items.
    If i return Qt::MoveAction, i am unable to drop the item anywhere in the tree view.

    Perhaps there is something wrong in the manupulation of the model data. I'll check and post back with result.

    With regards

Similar Threads

  1. Drag and Drop QTableWidget in UI file.
    By tpf80 in forum Qt Programming
    Replies: 3
    Last Post: 21st January 2009, 00:02
  2. drag and drop QToolButton in QToolBar
    By NBilal in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2008, 21:11
  3. Change cursor & status during Drag & Drop
    By ronlongo in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2008, 17:56
  4. Replies: 7
    Last Post: 8th September 2006, 17:19
  5. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 17:41

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.