4

How do I go about generating a file drag and drop operation in a Qt 5.2 application that performs a copy (or move) in the file system (Windows, Mac, etc.)? More specifically: Generate a drag with 1+ file paths that will perform a copy/move on those files when dropped onto the system.

Is there a standard mime-type I can use for this - providing file paths as mime data?

Prasad Silva
  • 1,020
  • 2
  • 11
  • 28

1 Answers1

0

QMimeData has a functionality to add a text/uri-list.

To drag/drop a file onto the system add a QUrl that starts with file:/// followed by your path.

QList<QUrl> urls;

list.append(QUrl("file:///path");

QMimeData* mimeData = new QMimeData;

mimeData->setUrls(list);
  • Could you provide some detail or explanation for your answer? – Joe Kennedy Jun 29 '14 at 18:28
  • The event made available throught the parameter list of dragEnterEvent has a member function mimeData(). Use this instead of creating a new QMimeData object that connects to nothing relevant. – user508402 Jun 05 '19 at 18:26
  • A quick piece of advice: You probably want to use `QUrl::fromLocalFile(const QString &localFile)` to create your `QUrl` instances. It abstracts away the complexity of encoding the path properly. – ssokolow Feb 07 '20 at 07:45