Results 1 to 3 of 3

Thread: Drag and drop revisited

  1. #1
    Join Date
    May 2006
    Posts
    28
    Thanks
    8
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Drag and drop revisited

    Hi, hoping some people could help me with some drag and drop of mime data

    I have a treeview and a standardItemModel I have subclassed.
    I'd like to drag and drop within the view to sort the items.
    And a line to indicate where would be perfect.

    I've looked at this thread and I've picked up lots of ideas.
    http://www.qtcentre.org/forum/f-qt-p...icon-2304.html

    However I cant make it work.. spent a few days trying

    If I implement only these :

    Qt Code:
    1. void MyTreeView::dragMoveEvent (QDragMoveEvent *event)
    2. {
    3.  
    4. dropSite = event->answerRect ( );
    5. model_->emit_layoutChanged();
    6. }
    7.  
    8. void MyModel::emit_layoutChanged ( )
    9. {
    10. emit layoutChanged ( );
    11. }
    12.  
    13. void MyTreeView::paintEvent ( QPaintEvent* event )
    14. {
    15. QTreeView::paintEvent (event);
    16. QPainter painter ( viewport() );
    17. int x, y, w, h;
    18. dropSite.getRect ( &x, &y, &w, &h );
    19. painter.drawLine ( 0, y, width(), y );
    20. event->accept();
    21. }
    To copy to clipboard, switch view to plain text mode 

    I get a line drawn although it doesnt stick to between items and draws behind at all pixel positions. However Its not a mime data drag and drop and so its a floating icon type which I want to avoid.

    Now, if I implement :


    Qt Code:
    1. void MyTreeView::mousePressEvent(QMouseEvent *event)
    2. {
    3. QTreeView::mousePressEvent ( event );
    4. if (event->button() == Qt::LeftButton) {
    5. dragStartPosition = event->pos();
    6. }
    7. repaint();
    8. }
    9.  
    10. void MyTreeView::mouseMoveEvent(QMouseEvent *event)
    11. {
    12. if (!(event->buttons() & Qt::LeftButton))
    13. return;
    14. if ((event->pos() - dragStartPosition).manhattanLength()
    15. < QApplication::startDragDistance())
    16. return;
    17.  
    18. QByteArray encodedData;
    19. mimeData = new QMimeData;
    20. // Sample text type mime data - I will use mybe row index to pass data instead
    21. mimeData->setData("application/vnd.text.list", encodedData);
    22.  
    23. drag = new QDrag(this);
    24. drag->setMimeData(mimeData);
    25. dropAction = drag->start ( Qt::CopyAction | Qt::MoveAction );
    26. repaint();
    27. }
    To copy to clipboard, switch view to plain text mode 

    I get a mime type drag and drop which I want but I cannot drop it on the Treeview anywhere. I get the no entry sign

    I have these turned on:

    treeView->setDragEnabled(true);
    treeView->setDropIndicatorShown(true);
    treeView->setAcceptDrops(true);

    I guess I need to implement something in here :
    Qt Code:
    1. bool MyModel::dropMimeData ( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent )
    2. {
    3. qDebug("dropMimeData");
    4. if (action == Qt::CopyAction) {
    5. return true;
    6. }
    7. else {
    8. return false;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    but this never gets called when I drop.

    What am I missing and any tips ? Mybe someone else find this useful.
    Thanks,

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

    Default Re: Drag and drop revisited

    It doesn't get called probably because you reimplemented the other methods. According to my belief, you should reimplement dropMimeData(), mimeData() and mimeTypes() from in model class.

  3. #3
    Join Date
    May 2006
    Posts
    28
    Thanks
    8
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag and drop revisited

    Hi, I made some great progress. I've a line drawn where the item is placed. And I can pass the row index of the item to be moved.

    May be useful for to someone, very similiar to Amarok(music player) playlist drag and drop.
    Without the actual moving of items yet.

    I guess a custom mime type would be best to stop unwanted drops and drags

    Qt Code:
    1. QStringList MyModel::mimeTypes () const
    2. {
    3. list << "text/plain";
    4. return list;
    5. }
    6.  
    7. void MyModel::emit_layoutChanged ( )
    8. {
    9. emit layoutChanged();
    10. }
    11.  
    12. void MyTreeView::mousePressEvent(QMouseEvent *event)
    13. {
    14. if (event->button() == Qt::LeftButton) {
    15. QTreeView::mousePressEvent(event);
    16. QModelIndex index = currentIndex();
    17. QString plainText = lexical_cast<std::string>(index.row()).c_str();
    18. mimeData = new QMimeData;
    19. mimeData->setText(plainText);
    20. drag = new QDrag(this);
    21. drag->setMimeData(mimeData);
    22. drag->setHotSpot(event->pos() - rect().topLeft());
    23. dropAction = drag->start(Qt::CopyAction | Qt::MoveAction);
    24. }
    25. }
    26.  
    27. void MyTreeView::dragEnterEvent(QDragEnterEvent *event)
    28. {
    29. if (event->mimeData()->hasText())
    30. event->acceptProposedAction();
    31. else
    32. event->ignore();
    33. }
    34.  
    35. void MyTreeView::dropEvent(QDropEvent *event)
    36. {
    37. if (event->mimeData()->hasText()) {
    38. event->setDropAction(Qt::MoveAction);
    39. std::string text = (event->mimeData()->text()).toStdString();
    40. std::cout << text << std::endl;
    41. }
    42. else {
    43. event->ignore();
    44. }
    45. }
    46.  
    47. void MyTreeView::dragMoveEvent (QDragMoveEvent *event)
    48. {
    49. dropSite = event->answerRect();
    50. model_->emit_layoutChanged(); // public function in model subclass
    51. }
    52.  
    53. void MyTreeView::paintEvent ( QPaintEvent* event )
    54. {
    55. QTreeView::paintEvent (event);
    56. QPainter painter ( viewport() );
    57. int x, y, w, h;
    58. dropSite.getRect ( &x, &y, &w, &h );
    59. QPoint point(x,y);
    60. QModelIndex modidx = indexAt ( point );
    61. QRect arect = visualRect ( modidx );
    62. int b = arect.y();
    63. QBrush brush(Qt::black, Qt::Dense4Pattern);
    64. QPen pen;
    65. pen.setWidth(2);
    66. pen.setBrush(brush);
    67. painter.setPen(pen);
    68. painter.drawLine ( 0, b, width(), b );
    69. event->accept();
    70. }
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to Big Duck for this useful post:

    vairamuthu.g (8th October 2010)

Similar Threads

  1. Drag & drop with model/view (Qt4)
    By yogeshm02 in forum Qt Programming
    Replies: 16
    Last Post: 19th September 2011, 20:36
  2. Drag and drop outside the application
    By jpn in forum Newbie
    Replies: 7
    Last Post: 27th August 2006, 15:37
  3. drag and drop
    By vijay anandh in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2006, 11:23
  4. Drag & drop for QTreeView
    By yogeshm02 in forum Qt Programming
    Replies: 2
    Last Post: 30th January 2006, 14:32
  5. Drag 'n Drop problem
    By kiker99 in forum Qt Programming
    Replies: 4
    Last Post: 16th January 2006, 16:35

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.