QList Drag and Drop Example: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
Example of the simple moving items using drag & drop betweeen two or more QListViews.
Example of the simple moving items using drag & drop betweeen two or more QListViews.


listbox.h:<br /><code>#ifndef LISTBOX_H<br />#define LISTBOX_H
listbox.h:
<code>#ifndef LISTBOX_H
#define LISTBOX_H


#include <QtGui>
#include <QtGui>


class ListBox : public QListWidget<br />{<br /> Q_OBJECT<br />public:<br /> ListBox(QWidget *parent);<br />protected:<br /> void dragMoveEvent(QDragMoveEvent *e);<br /> void dropEvent(QDropEvent *event);<br /> void startDrag(Qt::DropActions supportedActions);<br /> void dragEnterEvent(QDragEnterEvent *event);<br /> Qt::DropAction supportedDropActions();<br />signals:<br /> void itemDroped();<br />};
class ListBox : public QListWidget
{
Q_OBJECT
public:
ListBox(QWidget *parent);
protected:
void dragMoveEvent(QDragMoveEvent *e);
void dropEvent(QDropEvent *event);
void startDrag(Qt::DropActions supportedActions);
void dragEnterEvent(QDragEnterEvent *event);
Qt::DropAction supportedDropActions();
signals:
void itemDroped();
};


#endif // LISTBOX_H</code>
#endif // LISTBOX_H</code>


listbox.cpp:<br /><code>#include "listbox.h"
listbox.cpp:
<code>#include "listbox.h"


void ListBox::dragMoveEvent(QDragMoveEvent *e)<br />{<br /> if (e->mimeData()->hasFormat("application/x-item") &amp;&amp; e->source() != this) {<br /> e->setDropAction(Qt::MoveAction);<br /> e->accept();<br /> } else<br /> e->ignore();
void ListBox::dragMoveEvent(QDragMoveEvent *e)
{
if (e->mimeData()->hasFormat("application/x-item") &amp;&amp; e->source() != this) {
e->setDropAction(Qt::MoveAction);
e->accept();
} else
e->ignore();


}
}


ListBox::ListBox(QWidget *parent) : QListWidget(parent)<br />{<br /> this->setViewMode(QListView::IconMode);<br /> this->setIconSize(QSize(55, 55));<br /> this->setSelectionMode(QAbstractItemView::SingleSelection);<br /> this->setDragEnabled(true);<br /> this->setDefaultDropAction(Qt::MoveAction);<br /> this->setAcceptDrops(true);<br /> this->setDropIndicatorShown(true);<br />}
ListBox::ListBox(QWidget *parent) : QListWidget(parent)
{
this->setViewMode(QListView::IconMode);
this->setIconSize(QSize(55, 55));
this->setSelectionMode(QAbstractItemView::SingleSelection);
this->setDragEnabled(true);
this->setDefaultDropAction(Qt::MoveAction);
this->setAcceptDrops(true);
this->setDropIndicatorShown(true);
}


void ListBox::dropEvent(QDropEvent *event)<br />{<br /> if (event->mimeData()->hasFormat("application/x-item")) {<br /> event->accept();<br /> event->setDropAction(Qt::MoveAction);<br /> QListWidgetItem *item = new QListWidgetItem;<br /> QString name = event->mimeData()->data("application/x-item");<br /> item->setText(name);<br /> item->setIcon(QIcon(":/images/iString")); //set path to image<br /> addItem(item);<br /> }<br /> else<br /> event->ignore();<br />}
void ListBox::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasFormat("application/x-item")) {
event->accept();
event->setDropAction(Qt::MoveAction);
QListWidgetItem *item = new QListWidgetItem;
QString name = event->mimeData()->data("application/x-item");
item->setText(name);
item->setIcon(QIcon(":/images/iString")); //set path to image
addItem(item);
}
else
event->ignore();
}


void ListBox::startDrag(Qt::DropActions supportedActions)<br />{<br /> QListWidgetItem *item = currentItem();<br /> QMimeData *mimeData = new QMimeData;<br /> QByteArray ba;<br /> ba = item->text().toLatin1().data();<br /> mimeData->setData("application/x-item", ba);<br /> QDrag *drag = new QDrag(this);<br /> drag->setMimeData(mimeData);<br /> if (drag->exec(Qt::MoveAction) == Qt::MoveAction) {<br /> delete takeItem(row(item));<br /> emit itemDroped();<br /> }<br />}
void ListBox::startDrag(Qt::DropActions supportedActions)
{
QListWidgetItem *item = currentItem();
QMimeData *mimeData = new QMimeData;
QByteArray ba;
ba = item->text().toLatin1().data();
mimeData->setData("application/x-item", ba);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
if (drag->exec(Qt::MoveAction) == Qt::MoveAction) {
delete takeItem(row(item));
emit itemDroped();
}
}


void ListBox::dragEnterEvent(QDragEnterEvent *event)<br />{<br /> if (event->mimeData()->hasFormat("application/x-item"))<br /> event->accept();<br /> else<br /> event->ignore();<br />}
void ListBox::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("application/x-item"))
event->accept();
else
event->ignore();
}


Qt::DropAction ListBox::supportedDropActions()<br />{<br /> return Qt::MoveAction;<br />}</code>
Qt::DropAction ListBox::supportedDropActions()
{
return Qt::MoveAction;
}</code>

Revision as of 11:32, 25 February 2015

Example of the simple moving items using drag & drop betweeen two or more QListViews.

listbox.h:

#ifndef LISTBOX_H
#define LISTBOX_H

#include <QtGui>

class ListBox : public QListWidget
{
 Q_OBJECT
public:
 ListBox(QWidget *parent);
protected:
 void dragMoveEvent(QDragMoveEvent *e);
 void dropEvent(QDropEvent *event);
 void startDrag(Qt::DropActions supportedActions);
 void dragEnterEvent(QDragEnterEvent *event);
 Qt::DropAction supportedDropActions();
signals:
 void itemDroped();
};

#endif // LISTBOX_H

listbox.cpp:

#include "listbox.h"

void ListBox::dragMoveEvent(QDragMoveEvent *e)
{
 if (e->mimeData()->hasFormat("application/x-item") &amp;&amp; e->source() != this) {
 e->setDropAction(Qt::MoveAction);
 e->accept();
 } else
 e->ignore();

}

ListBox::ListBox(QWidget *parent) : QListWidget(parent)
{
 this->setViewMode(QListView::IconMode);
 this->setIconSize(QSize(55, 55));
 this->setSelectionMode(QAbstractItemView::SingleSelection);
 this->setDragEnabled(true);
 this->setDefaultDropAction(Qt::MoveAction);
 this->setAcceptDrops(true);
 this->setDropIndicatorShown(true);
}

void ListBox::dropEvent(QDropEvent *event)
{
 if (event->mimeData()->hasFormat("application/x-item")) {
 event->accept();
 event->setDropAction(Qt::MoveAction);
 QListWidgetItem *item = new QListWidgetItem;
 QString name = event->mimeData()->data("application/x-item");
 item->setText(name);
 item->setIcon(QIcon(":/images/iString")); //set path to image
 addItem(item);
 }
 else
 event->ignore();
}

void ListBox::startDrag(Qt::DropActions supportedActions)
{
 QListWidgetItem *item = currentItem();
 QMimeData *mimeData = new QMimeData;
 QByteArray ba;
 ba = item->text().toLatin1().data();
 mimeData->setData("application/x-item", ba);
 QDrag *drag = new QDrag(this);
 drag->setMimeData(mimeData);
 if (drag->exec(Qt::MoveAction) == Qt::MoveAction) {
 delete takeItem(row(item));
 emit itemDroped();
 }
}

void ListBox::dragEnterEvent(QDragEnterEvent *event)
{
 if (event->mimeData()->hasFormat("application/x-item"))
 event->accept();
 else
 event->ignore();
}

Qt::DropAction ListBox::supportedDropActions()
{
 return Qt::MoveAction;
}