Results 1 to 2 of 2

Thread: Drag and Drop

  1. #1
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question Drag and Drop

    I have a treeWidget that I am populating with positional data (3 columns x, y, z) that has several thousand rows. I am able to select each individual column and perform a drag and drop over to a table widget. This method works, but all of the data being exposed in the treeWidget is cumbersome. Now I would like to replace all of that data in my treeWidget with just having text that says "Position". I would then like the user to be able to select the "Position" text and drag and drop it over to the tableWidget with all of the data getting populated in the treeWidget. Does anyone know how to do this?? I think dragEnterEvent and dropEvent (maybe others?) would need reimplemented, but I'm not sure how to get the positional data to "attach" to the drag and drop event. Hope this is clear.

    Thanks.

    Qt 4.1.4 - MSVC 2005

  2. #2
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drag and Drop

    I actually just got done doing exactly what you need to do.

    You will need to over-ride mousePressEvent and mouseMoveEvent. Mouse move event is where you will determine if you have moved the mouse far enough to begin your dragging operation (draw dragging stuff and prep data to pass).

    Your class will need to have added:
    Qt Code:
    1. protected:
    2. void mousePressEvent(QMouseEvent *);
    3. void mouseMoveEvent(QMouseEvent *);
    4.  
    5. private:
    6. void startDrag(Qt::DropActions);
    7. QPoint startPos;
    To copy to clipboard, switch view to plain text mode 
    Then you'll need to implement the 3 functions:
    Qt Code:
    1. void YOUR_CLASS::mousePressEvent(QMouseEvent *event)
    2. {
    3. if (event->button() == Qt::LeftButton)
    4. startPos = event->pos();
    5. QTreeView::mousePressEvent(event); //let QTreeview further process event
    6. }
    7.  
    8. void YOUR_CLASS::mouseMoveEvent(QMouseEvent *event)
    9. {
    10. if(event->buttons() & Qt::LeftButton)
    11. {
    12. int distance = (event->pos() - startPos).manhattanLength();
    13. //have we moved far enough?
    14. if (distance >= QApplication::startDragDistance())
    15. startDrag(Qt::CopyAction | Qt::MoveAction | Qt::LinkAction); //start drag
    16. }
    17. QTreeView::mouseMoveEvent(event);
    18. }
    19.  
    20. void YOUR_CLASS::startDrag(Qt::DropActions supportedActions)
    21. {
    22. //make sure one or more items are selected, if not, return
    23. QModelIndexList indexes = this->selectedIndexes();
    24. if (indexes.count() > 0)
    25. {
    26. //Get the mimeData from your model. You will need to create
    27. ///this functionallity most likely.
    28. QMimeData *mimeData = model()->mimeData( indexes );
    29. QDrag *drag = new QDrag(this);
    30. drag->setMimeData( mimeData);
    31. drag->setPixmap( SOME_SWEET_ICON);
    32. /*I don't think you can just give it text to display.
    33. What shows up beside the drag is always a pixmap. Qt
    34. generates a pixmap of the selected stuff to use for the
    35. default drag operation. So if you want some specific text,
    36. you'll need to generate a pixmap of the text somehow
    37. and throw in the pixmap up in that function. Thats how
    38. you'll show text beside the cursor. */
    39.  
    40. //let Qt block until the drag operation completes
    41. drag->start(supportedActions);
    42. // -or-
    43. // if(drag->start(supportedActions) == Qt::CopyAction)
    44. // do something.....
    45. }
    46. }
    To copy to clipboard, switch view to plain text mode 

    for more help, you can reference an online Qt book here (search on "drag"):
    http://ebook-4u.ifastnet.com/c.htm

    Should help a little ;p
    Paul
    Last edited by thomaspu; 11th December 2006 at 21:54.

  3. The following user says thank you to thomaspu for this useful post:

    WinchellChung (30th October 2007)

Similar Threads

  1. Drag & drop with model/view (Qt4)
    By yogeshm02 in forum Qt Programming
    Replies: 16
    Last Post: 19th September 2011, 21:36
  2. Replies: 7
    Last Post: 8th September 2006, 17:19
  3. Drag and drop outside the application
    By jpn in forum Newbie
    Replies: 7
    Last Post: 27th August 2006, 16:37
  4. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 17:41
  5. Drag 'n Drop problem
    By kiker99 in forum Qt Programming
    Replies: 4
    Last Post: 16th January 2006, 17: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.