PDA

View Full Version : Drag and Drop items in QListWidget



tommynator128
12th January 2011, 13:47
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

JohannesMunk
12th January 2011, 17:02
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

tommynator128
26th January 2011, 19:37
.h

#ifndef FILELIST_H
#define FILELIST_H

#include <QtGui>
#include <QtCore>


class FileList : public QListWidget
{
Q_OBJECT

public:
FileList();

protected:
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dragLeaveEvent(QDragLeaveEvent *event);
void dropEvent(QDropEvent *event);
};

#endif

.cpp

#include <QtGui>

#include "fileList.h"



FileList::FileList()
{
setAlternatingRowColors(true);
dropHintItem = new QListWidgetItem;
dropHintItem->setText("Drop Files here...");
dropHintItem->setFlags(dropHintItem->flags() & ~(Qt::ItemIsDropEnabled));
insertItem(0, dropHintItem);
setAcceptDrops(true);
}

void FileList::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}

void FileList::dragMoveEvent(QDragMoveEvent *event)
{
event->acceptProposedAction();
}

void FileList::dropEvent(QDropEvent *event) {
const QMimeData *mimeData = event->mimeData();
if (mimeData->hasUrls()) {
//Insert the URLs and set flags to Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
}
event->acceptProposedAction();
}

void FileList::dragLeaveEvent(QDragLeaveEvent *event)
{
event->accept();
}

JohannesMunk
26th January 2011, 23:00
Hi!

Deep in the docs I found: http://doc.trolltech.com/latest/model-view-programming.html#using-drag-drop-with-item-views

The following seems to do what you want:


#include "FileList.h"

FileList::FileList()
{
setAcceptDrops(true);
setDragEnabled(true);

setSelectionMode(QAbstractItemView::SingleSelectio n);
setDropIndicatorShown(true);
setDragDropMode(QAbstractItemView::InternalMove);

setAlternatingRowColors(true);

dropHintItem = new QListWidgetItem("Drop Files here...",this);
}

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

void FileList::dragMoveEvent(QDragMoveEvent *event)
{
if (event->mimeData()->hasUrls())
{
event->acceptProposedAction();
} else {
QListWidget::dragMoveEvent(event);
}
}

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


Joh

abeinoe
10th March 2011, 11:09
thank..it's the solution for my problem on mac os. my old code worked fine on windows but not in mac.