user1767754
user1767754

Reputation: 25094

Drag and Drop multiple Objects in Qt

I can successfully drag and drop a Drag Object to any Application, but what is the correct way of dragging multiple items?

//Create Drag Object
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
QImage myImage = QImage(currentPath);
drag->setPixmap(QPixmap::fromImage(myImage, Qt::AutoColor));

//Send Source File to Target Application
mimeData->setText(this->getPathToSource());
drag->setMimeData(mimeData);

//Start drag/drop operation
drag->exec();

Upvotes: 4

Views: 2154

Answers (1)

Ezee
Ezee

Reputation: 4344

The clipboard can contain only one object at a time. But this object can be stored in different formats.

For example, a document can be stored as a text, as HTML and as Doc at the same time.

When you move a drag cursor over an application, it checks whether is is possible to drop an object or not using available formats and (rarely) data.

If you need to drag more than one object, you need to place data describing the objects in the clipboard using mimeData->setData(mimeType, data).
Where mimeType is a unique QString, for example, "mydatatype".
data is QByteArray wich information about the objects (or objects contents). For example, QStringList can be stored as following:

 QStringList list;
 mimeData->setData("myapplication::stringlist", list.join(",").toUtf8());

And here is the deserialization:

if (mimeData->hasFormat("myapplication::stringlist"))
{
   QStringList list = QString::fromUtf8(mimeData->data("myapplication::stringlist")).split(",");
}

Of course, you won't be able to drop such data in another (not yours) application.

EDIT:
When you drag files from Windows Explorer it places paths to files in the clipboard.
So if you want to drag for instance 2 images you'll have to save them in a temporary folder to use this way.

Windows Explorer places some mime-types to the clipboard. I think that the one you can use is text/uri-list. It is a list of file names. Each file name has format file:///path. Each file name starts from a new line.

Upvotes: 7

Related Questions