Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drag items in QListWidget while the mouse left button is pressed
Forum Updated to NodeBB v4.3 + New Features

Drag items in QListWidget while the mouse left button is pressed

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 4.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sargeslash
    wrote on 8 Mar 2013, 13:04 last edited by
    #1

    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.

    @#include"QtGui"
    #ifndef PROJECTLISTWIDGET_H
    #define PROJECTLISTWIDGET_H

    class ProjectListWidget : public QListWidget
    {
    Q_OBJECT
    public:
    ProjectListWidget(QWidget *parent = 0);

    signals:
    void itemDrag();

    protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void dragEnterEvent(QDragEnterEvent *event);
    void dragMoveEvent(QDragMoveEvent *event);
    void dropEvent(QDropEvent *event);
    void performDrag();
    QPoint startPos;
    };

    #endif // PROJECTLISTWIDGET_H
    @

    @#include "projectlistwidget.h"

    using namespace std;

    ProjectListWidget::ProjectListWidget(QWidget *parent)
    : QListWidget(parent)
    {
    setAcceptDrops(true);
    setDragEnabled(true);
    }

    void ProjectListWidget::mousePressEvent(QMouseEvent *event)
    {
    if (event->button() == Qt::LeftButton)
    {
    startPos = event->pos();
    }
    QListWidget::mousePressEvent(event);

    }

    void ProjectListWidget::mouseMoveEvent(QMouseEvent *event)
    {
    if (event->buttons() & Qt::LeftButton)
    {
    int distance = (event->pos() - startPos).manhattanLength();
    if (distance >= QApplication::startDragDistance())
    {
    performDrag();
    }
    }
    QListWidget::mouseMoveEvent(event);
    }

    void ProjectListWidget::performDrag()
    {
    QListWidgetItem *item = currentItem();
    if (item) {
    QMimeData *mimeData = new QMimeData;
    mimeData->setText(item->text());
    QDrag *drag = new QDrag(this);
    drag->setMimeData(mimeData);
    emit itemDrag();
    if (drag->exec(Qt::MoveAction) == Qt::CopyAction)
    delete item;
    }
    }

    void ProjectListWidget::dragEnterEvent(QDragEnterEvent *event)
    {
    if (event->mimeData()->hasUrls())
    {
    event->acceptProposedAction();
    } else
    {
    QListWidget::dragEnterEvent(event);
    }
    }

    void ProjectListWidget::dragMoveEvent(QDragMoveEvent *event)
    {
    if (event->mimeData()->hasUrls() & Qt::LeftButton)
    {
    event->acceptProposedAction();
    } else
    {
    QListWidget::dragMoveEvent(event);
    }
    }

    void ProjectListWidget::dropEvent(QDropEvent *event)
    {
    if (event->mimeData()->hasUrls())
    {
    QList<QUrl> urls = event->mimeData()->urls();
    if (!urls.isEmpty())
    {
    QUrl url;
    foreach (url,urls)
    {
    new QListWidgetItem(url.toLocalFile(),this);
    emit itemDrag();
    }
    }
    event->acceptProposedAction();
    }
    QListWidget::dropEvent(event);
    }

    @

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sargeslash
      wrote on 8 Mar 2013, 13:06 last edited by
      #2

      https://www.dropbox.com/s/fq9xm72hhchlb88/listbox.jpeg

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 8 Mar 2013, 13:34 last edited by
        #3

        Hi,

        do you want to only move the items within your view or also from one view to another ?

        In the first case you might be interested by "DragDropMode property":http://qt-project.org/doc/qt-4.8/qabstractitemview.html#dragDropMode-prop

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sargeslash
          wrote on 8 Mar 2013, 13:59 last edited by
          #4

          I am moving items between different views also(dragging items from QTreeView), all is working well but when I drag items inside the listbox I am facing this problem.

          1 Reply Last reply
          0

          1/4

          8 Mar 2013, 13:04

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved