Results 1 to 2 of 2

Thread: Drag items in QListWidget while the mouse left button is pressed

  1. #1
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60

    Default Drag items in QListWidget while the mouse left button is pressed

    I am having trouble with the dragging of items inside a QListWidget, the items which are selected are stuck with the mouse cursor even after the mouse button is released. I want to reorder the items inside the QListWidget by dragging them though mouse while the mouse left button is pressed.
    I have promoted my ui->listwidget to a custom listwidget to accept drag and drops.

    Qt Code:
    1. #ifndef PROJECTLISTWIDGET_H
    2. #define PROJECTLISTWIDGET_H
    3.  
    4. class ProjectListWidget : public QListWidget
    5. {
    6. Q_OBJECT
    7. public:
    8. ProjectListWidget(QWidget *parent = 0);
    9.  
    10. signals:
    11. void itemDrag();
    12.  
    13. protected:
    14. void mousePressEvent(QMouseEvent *event);
    15. void mouseMoveEvent(QMouseEvent *event);
    16. void dragEnterEvent(QDragEnterEvent *event);
    17. void dragMoveEvent(QDragMoveEvent *event);
    18. void dropEvent(QDropEvent *event);
    19. void performDrag();
    20. QPoint startPos;
    21. };
    22.  
    23.  
    24. #endif // PROJECTLISTWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "projectlistwidget.h"
    2.  
    3. using namespace std;
    4.  
    5. ProjectListWidget::ProjectListWidget(QWidget *parent)
    6. : QListWidget(parent)
    7. {
    8. setAcceptDrops(true);
    9. setDragEnabled(true);
    10. }
    11.  
    12. void ProjectListWidget::mousePressEvent(QMouseEvent *event)
    13. {
    14. if (event->button() == Qt::LeftButton)
    15. {
    16. startPos = event->pos();
    17. }
    18. QListWidget::mousePressEvent(event);
    19.  
    20. }
    21.  
    22. void ProjectListWidget::mouseMoveEvent(QMouseEvent *event)
    23. {
    24. if (event->buttons() & Qt::LeftButton)
    25. {
    26. int distance = (event->pos() - startPos).manhattanLength();
    27. if (distance >= QApplication::startDragDistance())
    28. {
    29. performDrag();
    30. }
    31. }
    32. QListWidget::mouseMoveEvent(event);
    33. }
    34.  
    35. void ProjectListWidget::performDrag()
    36. {
    37. QListWidgetItem *item = currentItem();
    38. if (item) {
    39. QMimeData *mimeData = new QMimeData;
    40. mimeData->setText(item->text());
    41. QDrag *drag = new QDrag(this);
    42. drag->setMimeData(mimeData);
    43. emit itemDrag();
    44. if (drag->exec(Qt::MoveAction) == Qt::CopyAction)
    45. delete item;
    46. }
    47. }
    48.  
    49. void ProjectListWidget::dragEnterEvent(QDragEnterEvent *event)
    50. {
    51. if (event->mimeData()->hasUrls())
    52. {
    53. event->acceptProposedAction();
    54. } else
    55. {
    56. QListWidget::dragEnterEvent(event);
    57. }
    58. }
    59.  
    60. void ProjectListWidget::dragMoveEvent(QDragMoveEvent *event)
    61. {
    62. if (event->mimeData()->hasUrls() & Qt::LeftButton)
    63. {
    64. event->acceptProposedAction();
    65. } else
    66. {
    67. QListWidget::dragMoveEvent(event);
    68. }
    69. }
    70.  
    71. void ProjectListWidget::dropEvent(QDropEvent *event)
    72. {
    73. if (event->mimeData()->hasUrls())
    74. {
    75. QList<QUrl> urls = event->mimeData()->urls();
    76. if (!urls.isEmpty())
    77. {
    78. QUrl url;
    79. foreach (url,urls)
    80. {
    81. new QListWidgetItem(url.toLocalFile(),this);
    82. emit itemDrag();
    83. }
    84. }
    85. event->acceptProposedAction();
    86. }
    87. QListWidget::dropEvent(event);
    88. }
    To copy to clipboard, switch view to plain text mode 

    listbox.jpeg

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 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 items in QListWidget while the mouse left button is pressed

    Why don't you just use the QAbstractItemView::dragDropMode property?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to detect hover events when mouse button is pressed
    By yagabey in forum Qt Programming
    Replies: 12
    Last Post: 26th April 2016, 10:23
  2. Drag Text on QPainter with left button of mouse movement
    By amitpatel22 in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2011, 13:52
  3. Replies: 4
    Last Post: 29th August 2010, 19:16
  4. combining Alt + Left Mouse Button
    By speedracer in forum Qt Programming
    Replies: 1
    Last Post: 3rd June 2009, 14:29
  5. Replies: 2
    Last Post: 13th May 2009, 20:01

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.