QMimeData 【Qt帮助文档】

本文介绍了QMimeData类的使用方法,包括如何通过QMimeData处理URL拖拽、自定义数据存储方式以及跨平台MIME类型的支持。QMimeData是Qt框架中用于拖放操作的数据载体。

 

http://doc.qt.nokia.com/latest/qmimedata.html

 

QMimeData Class Reference

 

For example, if your write a widget that accepts URL drags, you would end up writing code like this:

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

 void MyWidget::dropEvent(QDropEvent *event)
 {
     if (event->mimeData()->hasUrls()) {
         foreach (QUrl url, event->mimeData()->urls()) {
             ...
         }
     }
 }

There are three approaches for storing custom data in a QMimeData object:

  1. Custom data can be stored directly in a QMimeData object as a QByteArray using setData(). For example:
     QByteArray csvData = ...;
    
     QMimeData *mimeData = new QMimeData;
     mimeData->setData("text/csv", csvData);
  2. We can subclass QMimeData and reimplement hasFormat(), formats(), and retrieveData().
  3. If the drag and drop operation occurs within a single application, we can subclass QMimeData and add extra data in it, and use a qobject_cast() in the receiver's drop event handler. For example:
     void MyWidget::dropEvent(QDropEvent *event)
     {
         const MyMimeData *myData =
                 qobject_cast<const MyMimeData *>(event->mimeData());
         if (myData) {
             // access myData's data directly (not through QMimeData's API)
         }
     }

Platform-Specific MIME Types

On Windows, formats() will also return custom formats available in the MIME data, using the x-qt-windows-mime subtype to indicate that they represent data in non-standard formats. The formats will take the following form:

 application/x-qt-windows-mime;value="<custom type>"

The following are examples of custom MIME types:

 application/x-qt-windows-mime;value="FileGroupDescriptor"
 application/x-qt-windows-mime;value="FileContents"

The value declaration of each format describes the way in which the data is encoded.

On Windows, the MIME format does not always map directly to the clipboard formats. Qt provides QWindowsMime to map clipboard formats to open-standard MIME formats. Similarly, the QMacPasteboardMime maps MIME to Mac flavors.

See also QClipboard, QDragEnterEvent, QDragMoveEvent, QDropEvent, QDrag, QWindowsMime, QMacPasteboardMime, and Drag and Drop.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值