QMimeData

Inheritance diagram of QMimeData

Synopsis

Functions

Virtual functions

Detailed Description

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

PySide.QtCore.QMimeData is used to describe information that can be stored in the clipboard , and transferred via the drag and drop mechanism. PySide.QtCore.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.

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

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

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

Tester Getter Setter MIME Types
PySide.QtCore.QMimeData.hasText() PySide.QtCore.QMimeData.text() PySide.QtCore.QMimeData.setText() text/plain
PySide.QtCore.QMimeData.hasHtml() PySide.QtCore.QMimeData.html() PySide.QtCore.QMimeData.setHtml() text/html
PySide.QtCore.QMimeData.hasUrls() PySide.QtCore.QMimeData.urls() PySide.QtCore.QMimeData.setUrls() text/uri-list
PySide.QtCore.QMimeData.hasImage() PySide.QtCore.QMimeData.imageData() PySide.QtCore.QMimeData.setImageData() image/ *
PySide.QtCore.QMimeData.hasColor() PySide.QtCore.QMimeData.colorData() PySide.QtCore.QMimeData.setColorData() application/x-color

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

def dragEnterEvent(self, event):
    if event.mimeData().hasUrls():
        event.acceptProposedAction()

def dropEvent(self, event):
    if event->mimeData().hasUrls():
        for url in event.mimeData().urls():
            ...

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

Platform-Specific MIME Types

On Windows, PySide.QtCore.QMimeData.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

PySide.QtGui.QClipboard PySide.QtGui.QDragEnterEvent PySide.QtGui.QDragMoveEvent PySide.QtGui.QDropEvent PySide.QtGui.QDrag QWindowsMime QMacPasteboardMime Drag and Drop

class PySide.QtCore.QMimeData

Constructs a new MIME data object with no data in it.

PySide.QtCore.QMimeData.clear()

Removes all the MIME type and data entries in the object.

PySide.QtCore.QMimeData.colorData()
Return type:object

Returns a color if the data stored in the object represents a color (MIME type application/x-color ); otherwise returns a null variant.

A PySide.QtCore.QVariant is used because PySide.QtCore.QMimeData belongs to the QtCore library, whereas PySide.QtGui.QColor belongs to QtGui . To convert the PySide.QtCore.QVariant to a PySide.QtGui.QColor , simply use qvariant_cast() . For example:

if event.mimeData().hasColor():
    color = QColor(event.mimeData().colorData())
    ...
PySide.QtCore.QMimeData.data(mimetype)
Parameters:mimetype – unicode
Return type:PySide.QtCore.QByteArray

Returns the data stored in the object in the format described by the MIME type specified by mimeType .

PySide.QtCore.QMimeData.formats()
Return type:list of strings

Returns a list of formats supported by the object. This is a list of MIME types for which the object can return suitable data. The formats in the list are in a priority order.

For the most common types of data, you can call the higher-level functions PySide.QtCore.QMimeData.hasText() , PySide.QtCore.QMimeData.hasHtml() , PySide.QtCore.QMimeData.hasUrls() , PySide.QtCore.QMimeData.hasImage() , and PySide.QtCore.QMimeData.hasColor() instead.

PySide.QtCore.QMimeData.hasColor()
Return type:PySide.QtCore.bool

Returns true if the object can return a color (MIME type application/x-color ); otherwise returns false.

PySide.QtCore.QMimeData.hasFormat(mimetype)
Parameters:mimetype – unicode
Return type:PySide.QtCore.bool

Returns true if the object can return data for the MIME type specified by mimeType ; otherwise returns false.

For the most common types of data, you can call the higher-level functions PySide.QtCore.QMimeData.hasText() , PySide.QtCore.QMimeData.hasHtml() , PySide.QtCore.QMimeData.hasUrls() , PySide.QtCore.QMimeData.hasImage() , and PySide.QtCore.QMimeData.hasColor() instead.

PySide.QtCore.QMimeData.hasHtml()
Return type:PySide.QtCore.bool

Returns true if the object can return HTML (MIME type text/html ); otherwise returns false.

PySide.QtCore.QMimeData.hasImage()
Return type:PySide.QtCore.bool

Returns true if the object can return an image; otherwise returns false.

PySide.QtCore.QMimeData.hasText()
Return type:PySide.QtCore.bool

Returns true if the object can return plain text (MIME type text/plain ); otherwise returns false.

PySide.QtCore.QMimeData.hasUrls()
Return type:PySide.QtCore.bool

Returns true if the object can return a list of urls; otherwise returns false.

URLs correspond to the MIME type text/uri-list .

PySide.QtCore.QMimeData.html()
Return type:unicode

Returns a string if the data stored in the object is HTML (MIME type text/html ); otherwise returns an empty string.

PySide.QtCore.QMimeData.imageData()
Return type:object

Returns a PySide.QtCore.QVariant storing a PySide.QtGui.QImage if the object can return an image; otherwise returns a null variant.

A PySide.QtCore.QVariant is used because PySide.QtCore.QMimeData belongs to the QtCore library, whereas PySide.QtGui.QImage belongs to QtGui . To convert the PySide.QtCore.QVariant to a PySide.QtGui.QImage , simply use qvariant_cast() . For example:

if event.mimeData().hasImage():
    image = QImage(event.mimeData().imageData())
    ...
PySide.QtCore.QMimeData.removeFormat(mimetype)
Parameters:mimetype – unicode

Removes the data entry for mimeType in the object.

PySide.QtCore.QMimeData.retrieveData(mimetype, preferredType)
Parameters:
  • mimetype – unicode
  • preferredTypePySide.QtCore.QVariant::Type
Return type:

object

PySide.QtCore.QMimeData.setColorData(color)
Parameters:color – object

Sets the color data in the object to the given color .

Colors correspond to the MIME type application/x-color .

PySide.QtCore.QMimeData.setData(mimetype, data)
Parameters:

Sets the data associated with the MIME type given by mimeType to the specified data .

For the most common types of data, you can call the higher-level functions PySide.QtCore.QMimeData.setText() , PySide.QtCore.QMimeData.setHtml() , PySide.QtCore.QMimeData.setUrls() , PySide.QtCore.QMimeData.setImageData() , and PySide.QtCore.QMimeData.setColorData() instead.

Note that if you want to use a custom data type in an item view drag and drop operation, you must register it as a Qt meta type , using the Q_DECLARE_METATYPE() macro, and implement stream operators for it. The stream operators must then be registered with the qRegisterMetaTypeStreamOperators() function.

See also

PySide.QtCore.QMimeData.data() PySide.QtCore.QMimeData.hasFormat() QMetaType qRegisterMetaTypeStreamOperators()

PySide.QtCore.QMimeData.setHtml(html)
Parameters:html – unicode

Sets html as the HTML (MIME type text/html ) used to represent the data.

PySide.QtCore.QMimeData.setImageData(image)
Parameters:image – object

Sets the data in the object to the given image .

A PySide.QtCore.QVariant is used because PySide.QtCore.QMimeData belongs to the QtCore library, whereas PySide.QtGui.QImage belongs to QtGui . The conversion from PySide.QtGui.QImage to PySide.QtCore.QVariant is implicit. For example:

mimeData.setImageData(QImage("beautifulfjord.png"))
PySide.QtCore.QMimeData.setText(text)
Parameters:text – unicode

Sets text as the plain text (MIME type text/plain ) used to represent the data.

PySide.QtCore.QMimeData.setUrls(urls)
Parameters:urls
PySide.QtCore.QMimeData.text()
Return type:unicode

Returns a plain text (MIME type text/plain ) representation of the data.

PySide.QtCore.QMimeData.urls()
Return type:

Returns a list of URLs contained within the MIME data object.

URLs correspond to the MIME type text/uri-list .