QList Drag and Drop Example: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
m (Wieland moved page Dragndrop QList move example to QList Drag and Drop Example: underscores)
(Cleanup)
 
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
 
Example of the simple moving items using drag and drop betweeen two or more QListViews.
Example of the simple moving items using drag & drop betweeen two or more QListViews.


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


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


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


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


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


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


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


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


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


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

Latest revision as of 16:54, 28 June 2015

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

Example of the simple moving items using drag and 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") && 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;
}