Results 1 to 2 of 2

Thread: Drag and drop in QTreeView with custom QAbstractItem based model

  1. #1
    Join Date
    Mar 2014
    Location
    Linz, Austria
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Drag and drop in QTreeView with custom QAbstractItem based model

    Hi folks!

    We want to implement drag-drop functionality for our QTreeView which uses a custom QAbstractItem based model.

    I've created a QDragableTreeView class which is derived from QTreeView.

    The problem is that the implementation works up to the point when the mouse button is released (and the change in order should happen). Infact nothing happens. My question is how can i switch the order of my elements in the tree at the end of the drag?

    Header of QDragableTreeView
    Qt Code:
    1. #ifndef QDRAGABLETREEVIEW_H
    2. #define QDRAGABLETREEVIEW_H
    3.  
    4. #include <QTreeView>
    5.  
    6. class QDragableTreeView : public QTreeView
    7. {
    8. Q_OBJECT
    9.  
    10. void mousePressEvent(QMouseEvent *event);
    11. void mouseMoveEvent(QMouseEvent *event);
    12. void performDrag(void);
    13. void dragEnterEvent ( QDragEnterEvent * event );
    14. void dragMoveEvent(QDragMoveEvent *event);
    15. void dropEvent(QDropEvent *event);
    16.  
    17.  
    18. public:
    19. explicit QDragableTreeView(QWidget *parent = 0);
    20.  
    21.  
    22. signals:
    23.  
    24. public slots:
    25.  
    26. };
    27.  
    28. #endif // QDRAGABLETREEVIEW_H
    To copy to clipboard, switch view to plain text mode 


    PerformDrag implementation:
    Qt Code:
    1. void QDragableTreeView::performDrag()
    2. {
    3. QModelIndex idx = currentIndex();
    4. PositionsTreeModel *model = static_cast<PositionsTreeModel*>(this->model());
    5. if(model){
    6. qDebug() << "performDrag";
    7. //QStandardItem *sourceItem = model->itemFromIndex(idx);
    8. QMimeData *mimeData = new QMimeData();
    9. mimeData->setText("asdf");
    10.  
    11. QDrag *drag = new QDrag(this);
    12. drag->setMimeData(mimeData);
    13.  
    14. if(drag->exec(Qt::MoveAction) == Qt::MoveAction){
    15. //emit layoutAboutToBeChanged();
    16. //model->removeRow(idx.row());
    17. //model->deleteRow(idx);
    18.  
    19. //emit rowsAboutToBeRemoved(idx.parent(),0,3);
    20. model->removeRow(0, idx);
    21.  
    22. emit dataChanged(rootIndex(), idx);
    23. qDebug() << "deletingItem...";
    24. qDebug() << "row: " << idx.row();
    25. //emit layoutChanged();
    26. }
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    kind regards, reinhart

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Drag and drop in QTreeView with custom QAbstractItem based model

    One thing that is problematic with the code is that it uses a QModelIndex instance ("idx"), after doing event loop process (call to drag->exec()).

    It might be OK dependning on the model, but the correct way to "keep" a model index around is to convert it into a QPersistentModelIndex.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 8th January 2011, 12:04
  2. Replies: 1
    Last Post: 2nd August 2010, 20:16
  3. QTreeView and QAbstractItem Model.
    By Terabyte in forum Qt Programming
    Replies: 1
    Last Post: 13th January 2009, 13:18
  4. drag and drop from QTreeView
    By Untersander in forum Qt Programming
    Replies: 1
    Last Post: 10th April 2006, 09:00
  5. Drag & drop for QTreeView
    By yogeshm02 in forum Qt Programming
    Replies: 2
    Last Post: 30th January 2006, 14:32

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.