Results 1 to 5 of 5

Thread: Drag and Drop items in QListWidget

  1. #1
    Join Date
    Jan 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Drag and Drop items in QListWidget

    Hello,

    i've got an list widget that should be able to accept file drops and be able of drag and drop the items into the desired order. if i do all this stuff and remove my own drop event functions it still duplicates the item. if i got my own drop event function there is no possibility of dropping files. how should i do this?

    thanks
    tommy

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag and Drop items in QListWidget

    Hi Tommy!

    I find it hard to guess what you might have done wrong. Could you post your source code? Preferably just the relevant but still compilable parts.

    Johannes

  3. #3
    Join Date
    Jan 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Drag and Drop items in QListWidget

    .h
    Qt Code:
    1. #ifndef FILELIST_H
    2. #define FILELIST_H
    3.  
    4. #include <QtGui>
    5. #include <QtCore>
    6.  
    7.  
    8. class FileList : public QListWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. FileList();
    14.  
    15. protected:
    16. void dragEnterEvent(QDragEnterEvent *event);
    17. void dragMoveEvent(QDragMoveEvent *event);
    18. void dragLeaveEvent(QDragLeaveEvent *event);
    19. void dropEvent(QDropEvent *event);
    20. };
    21.  
    22. #endif
    To copy to clipboard, switch view to plain text mode 

    .cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "fileList.h"
    4.  
    5.  
    6.  
    7. FileList::FileList()
    8. {
    9. setAlternatingRowColors(true);
    10. dropHintItem = new QListWidgetItem;
    11. dropHintItem->setText("Drop Files here...");
    12. dropHintItem->setFlags(dropHintItem->flags() & ~(Qt::ItemIsDropEnabled));
    13. insertItem(0, dropHintItem);
    14. setAcceptDrops(true);
    15. }
    16.  
    17. void FileList::dragEnterEvent(QDragEnterEvent *event)
    18. {
    19. event->acceptProposedAction();
    20. }
    21.  
    22. void FileList::dragMoveEvent(QDragMoveEvent *event)
    23. {
    24. event->acceptProposedAction();
    25. }
    26.  
    27. void FileList::dropEvent(QDropEvent *event) {
    28. const QMimeData *mimeData = event->mimeData();
    29. if (mimeData->hasUrls()) {
    30. //Insert the URLs and set flags to Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
    31. }
    32. event->acceptProposedAction();
    33. }
    34.  
    35. void FileList::dragLeaveEvent(QDragLeaveEvent *event)
    36. {
    37. event->accept();
    38. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag and Drop items in QListWidget

    Hi!

    Deep in the docs I found: http://doc.trolltech.com/latest/mode...ith-item-views

    The following seems to do what you want:

    Qt Code:
    1. #include "FileList.h"
    2.  
    3. FileList::FileList()
    4. {
    5. setAcceptDrops(true);
    6. setDragEnabled(true);
    7.  
    8. setSelectionMode(QAbstractItemView::SingleSelection);
    9. setDropIndicatorShown(true);
    10. setDragDropMode(QAbstractItemView::InternalMove);
    11.  
    12. setAlternatingRowColors(true);
    13.  
    14. dropHintItem = new QListWidgetItem("Drop Files here...",this);
    15. }
    16.  
    17. void FileList::dragEnterEvent(QDragEnterEvent *event)
    18. {
    19. if (event->mimeData()->hasUrls())
    20. {
    21. event->acceptProposedAction();
    22. } else {
    23. QListWidget::dragEnterEvent(event);
    24. }
    25. }
    26.  
    27. void FileList::dragMoveEvent(QDragMoveEvent *event)
    28. {
    29. if (event->mimeData()->hasUrls())
    30. {
    31. event->acceptProposedAction();
    32. } else {
    33. QListWidget::dragMoveEvent(event);
    34. }
    35. }
    36.  
    37. void FileList::dropEvent(QDropEvent *event)
    38. {
    39. if (event->mimeData()->hasUrls())
    40. {
    41. QList<QUrl> urls = event->mimeData()->urls();
    42. if (!urls.isEmpty())
    43. {
    44. if (dropHintItem)
    45. {
    46. delete dropHintItem;
    47. dropHintItem = 0;
    48. }
    49. QUrl url;
    50. foreach (url,urls)
    51. {
    52. new QListWidgetItem(url.toLocalFile(),this);
    53. }
    54. }
    55. event->acceptProposedAction();
    56. }
    57. QListWidget::dropEvent(event);
    58. }
    To copy to clipboard, switch view to plain text mode 

    Joh

  5. #5
    Join Date
    Jan 2009
    Posts
    11

    Default Re: Drag and Drop items in QListWidget

    thank..it's the solution for my problem on mac os. my old code worked fine on windows but not in mac.

Similar Threads

  1. Drag and drop between QListWidget's
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2011, 05:29
  2. drag&drop from QGraphicsScene to QListWidget
    By hakiim35 in forum Qt Programming
    Replies: 4
    Last Post: 7th July 2010, 12:29
  3. Replies: 3
    Last Post: 10th June 2010, 16:13
  4. QListWidget/QTreeWidget Drag and Drop
    By hlvietlong in forum Qt Programming
    Replies: 2
    Last Post: 30th June 2009, 19:09
  5. Drag from QTreeWidget and drop in QListWidget
    By WXNSNW in forum Qt Programming
    Replies: 1
    Last Post: 25th August 2008, 01:15

Tags for this Thread

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.