简体   繁体   English

在Qt中拖放多个对象

[英]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();

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. 例如,文档可以同时作为HTML和Doc存储为文本。

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) . 如果需要拖动多个对象,则需要使用mimeData->setData(mimeType, data)将描述对象的数据放在剪贴板中。
Where mimeType is a unique QString , for example, "mydatatype". 其中mimeType是唯一的QString ,例如“mydatatype”。
data is QByteArray wich information about the objects (or objects contents). dataQByteArray ,其中包含有关对象(或对象内容)的信息。 For example, QStringList can be stored as following: 例如, QStringList可以存储如下:

 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. 从Windows资源管理器中拖动文件时,它会将路径放置到剪贴板中的文件。
So if you want to drag for instance 2 images you'll have to save them in a temporary folder to use this way. 因此,如果要拖动实例2图像,则必须将它们保存在临时文件夹中才能使用这种方式。

Windows Explorer places some mime-types to the clipboard. Windows资源管理器将一些mime类型放置到剪贴板。 I think that the one you can use is text/uri-list . 我认为你可以使用的是text/uri-list It is a list of file names. 它是文件名列表。 Each file name has format file:///path . 每个文件名都有格式file:///path Each file name starts from a new line. 每个文件名都从一个新行开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM