Home · Overviews · Examples 

QMimeData Class Reference
[com.trolltech.qt.gui module]

The QMimeData class provides a container for data that records information about its MIME type. More...

Inherits QObject.


Detailed Description

The QMimeData class provides a container for data that records information about its MIME type.

QMimeData is used to describe information that can be stored in the clipboard, and transferred via the drag and drop mechanism. QMimeData objects associate the data that they hold with the corresponding MIME types to ensure that information can be safely transferred between applications, and copied around within the same application.

QMimeData objects are usually created using new and supplied to QDrag or QClipboard objects. This is to enable Qt to manage the memory that they use.

A single QMimeData object can store the same data using several different formats at the same time. The formats function returns a list of the available formats in order of preference. The data function returns the raw data associated with a MIME type, and setData allows you to set the data for a MIME type.

For the most common MIME types, QMimeData provides convenience functions to access the data:

TesterGetterSetterMIME Types
hasTexttextsetTexttext/plain
hasHtmlhtmlsetHtmltext/html
hasUrlsurlssetUrlstext/uri-list
hasImageimageDatasetImageDataimage/ *
hasColorcolorDatasetColorDataapplication/x-color

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()) {
            QUrl url = event->mimeData()->urls();
            ...
        }
    }

There are three appraches 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 withing 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.


Copyright © 2007 Trolltech Trademarks
Qt Jambi 4.3.2_01