Results 1 to 8 of 8

Thread: Drag Drop Help pls

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Drag Drop Help pls

    Hi,

    I have started to implement a drag n drop operation from QListWidget to QTreeWidget. I have implemented the drag operation but it is not working. Here is my code.

    Qt Code:
    1. void NameListWidget::mousePressEvent(QMouseEvent *event)
    2. {
    3. if(event->buttons() & Qt::LeftButton){
    4. startPos = event->pos();
    5. }
    6. QListWidget::mousePressEvent(event);
    7. }
    8.  
    9. void NameListWidget::mouseMoveEvent(QMouseEvent *event)
    10. {
    11. if(event->buttons() & Qt::LeftButton){
    12. int xDiff = startPos.x() - event->pos().x();
    13.  
    14. if(xDiff > 3 ){//drag only if the movement is towards left
    15. startDrag();
    16. }
    17. }
    18. QListWidget::mouseMoveEvent(event);
    19. }
    20.  
    21. void NameListWidget::startDrag()
    22. {
    23. QListWidgetItem * items = currentItem();
    24. if(item){
    25. QMimeData *mimeData = new QMimeData;
    26. mimeData->setText(item->text());
    27. QDrag *drag = new QDrag(this);
    28. drag->setMimeData(mimeData);
    29. drag->setPixmap(QPixmap(":/images/personsmall.png"));
    30. drag->setHotSpot(QPoint(drag->pixmap().width()/2,
    31. drag->pixmap().height()));
    32. drag->start();
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 

    When I start dragging the pixmap does not appear. Can someone please tell what can be the problem ?

    Thanks a lot.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 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 Help pls

    But the drag itself is fine? Can you make a successfull drop with it? Is the pixmap accessible?

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag Drop Help pls

    The pixmap is accessible and is right. I have not yet implemented the drop. I first wanted to check if the drag works fine. When I try to drag I get the forbiddeb cursor.

    Any idea?

    Thanks a lot.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 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 Help pls

    Well... in my opinion the code looks fine. What happens if you resign from reimplementing startDrag() and encode the mime data through QListWidget::mimeData()? You should get a proper drag with a picture of your items as a pixmap. Does that work? Have you confirmed that the pixmap is there in the resource by loading it onto a QLabel or something like that?

  5. #5
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag Drop Help pls

    Sorry for getting back to this so late, but I have not yet got this working. Can someone please help

    Quote Originally Posted by wysota
    What happens if you resign from reimplementing startDrag() and encode the mime data through QListWidget::mimeData()?
    Can you tell me more on this ?

    Quote Originally Posted by wysota
    Have you confirmed that the pixmap is there in the resource by loading it onto a QLabel or something like that?
    Yes, I have confirmed. I use the same pixmap in couple of other places in my application. It appears in the other places.

    Please Help
    Thanks a lot

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 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 Help pls

    I meant that you should try something simpler, more straightforward. If you teach your widget to encode items according to a specific mime (for example the text you are using now) and teach your tree widget to accept those, you should get a working drag&drop. You can then experiment more with startDrag and custom pixmaps.

  7. #7
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag Drop Help pls

    I added the line setDragEnabled(true) in the constructor of ListWidget, commented my code for dragging and it kind of works. But I am looking for something different.

    When I start dragging, I get forbidden cursor when the drag is still inside the QListWidget. When the drag is inside the QTreeWidget is shows correctly (both icon and text). QTreeWidget accepts the drop and the item is added as a top level item.

    I would like to show what is being dragged while inside the ListWidget also. Apart from that I do not want a new top level item being added to the treewidget when dropped but something else.

    Forget about the drop we can see that later.

    Quote Originally Posted by wysota
    If you teach your widget to encode items according to a specific mime (for example the text you are using now) and teach your tree widget to accept those, you should get a working drag&drop.
    Can you please give a simple example ?

    Thanks a lot.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 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 Help pls

    Quote Originally Posted by munna
    I would like to show what is being dragged while inside the ListWidget also.
    Make your list items return flag Qt::ItemIsDropEnabled and set acceptDrops of the widget to true.
    Apart from that I do not want a new top level item being added to the treewidget when dropped but something else.
    I'm not sure how to do that in Q*Widgets... using the model approach you should reimplement dropMimeData() and implement desired functionality there.

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 revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 16:41
  4. Drag and Drop (drop example)
    By din9 in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2006, 18:03
  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.