PDA

View Full Version : drag and drop: drag item from QListWidget to desktop or any open directory



Vovasty
29th January 2014, 14:40
Hi everyone!
I have QListWidget who contains list of files path. This files i'm dropped from explorer,
but i don't know how to implement drag and drop from QListWidget to the desktop or explorer.
This is my try...


void FileListWidget::startDrag()
{
if (currentItem()) {
QByteArray data;
QFile src(currentItem()->text());
src.open(QIODevice::ReadOnly);
data = src.readAll();
src.close();
QMimeData *mime = new QMimeData;
mime->setData("text/uri-list", data);

QDrag *drag = new QDrag(this);
drag->setMimeData(mime);
drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
}
}

... but it's not working (error when copying file). Please explain me, what should I do to solve this problem?

P.S. Sorry for my bad English:o

anda_skoa
29th January 2014, 18:44
Ah, only a minor misunderstanding.

You don't have to read the file and send its contents, you just send its filename.

Something like this



QFIleInfo fileInfo(currentItem()->text());
QUrl url = QUrl::fromLocalFile(fileInfo.absoluteFilePath());

QMimeData *mime = new QMimeData;
mime->setUrls(QList<QUrl>() << url);

...


Cheers,
_

Vovasty
30th January 2014, 06:46
Thank you very much for help... it's work! :)