PySide6.QtCore.QMimeData¶
- class QMimeData¶
- The - QMimeDataclass provides a container for data that records information about its MIME type. More…- Synopsis¶- Methods¶- def - __init__()
- def - clear()
- def - colorData()
- def - data()
- def - hasColor()
- def - hasHtml()
- def - hasImage()
- def - hasText()
- def - hasUrls()
- def - html()
- def - imageData()
- def - removeFormat()
- def - setColorData()
- def - setData()
- def - setHtml()
- def - setImageData()
- def - setText()
- def - setUrls()
- def - text()
- def - urls()
 - Virtual methods¶- def - formats()
- def - hasFormat()
- def - retrieveData()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - QMimeDatais used to describe information that can be stored in the clipboard, and transferred via the drag and drop mechanism.- QMimeDataobjects 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.- QMimeDataobjects are usually created using- newand supplied to QDrag or QClipboard objects. This is to enable Qt to manage the memory that they use.- A single - QMimeDataobject 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, - QMimeDataprovides convenience functions to access the data:- Tester - Getter - Setter - MIME Types - text/plain- text/html- text/uri-list- image/*- 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 - QMimeDataobject:- Custom data can be stored directly in a - QMimeDataobject as a- QByteArrayusing- setData(). For example:- csvData = ... mimeData = QMimeData() mimeData.setData("text/csv", csvData) 
- We can subclass - QMimeDataand reimplement- hasFormat(),- formats(), and- retrieveData().
- If the drag and drop operation occurs within a single application, we can subclass - QMimeDataand add extra data in it, and use a- qobject_cast()in the receiver’s drop event handler. For example:- def dropEvent(self, event): myData = 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-mimesubtype 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 - valuedeclaration of each format describes the way in which the data is encoded.- In some cases (e.g. dropping multiple email attachments), multiple data values are available. They can be accessed by adding an - indexvalue:- application/x-qt-windows-mime;value="FileContents";index=0 application/x-qt-windows-mime;value="FileContents";index=1 - On Windows, the MIME format does not always map directly to the clipboard formats. Qt provides QWindowsMimeConverter to map clipboard formats to open-standard MIME formats. Similarly, the QUtiMimeConverter maps MIME to Uniform Type Identifiers on macOS and iOS. - See also - QClipboardQDragEnterEventQDragMoveEventQDropEventQDragDrag and Drop- __init__()¶
 - Constructs a new MIME data object with no data in it. - clear()¶
 - Removes all the MIME type and data entries in the object. - colorData()¶
- Return type:
- object 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Returns a color if the data stored in the object represents a color (MIME type - application/x-color); otherwise returns a null variant.- A - QVariantis used because- QMimeDatabelongs to the Qt Core module, whereas QColor belongs to Qt GUI. To convert the- QVariantto a QColor, simply use- qvariant_cast(). For example:- if event.mimeData().hasColor(): color = QColor(event.mimeData().colorData()) ... - See also - data(mimetype)¶
- Parameters:
- mimetype – str 
- Return type:
 
 - Returns the data stored in the object in the format described by the MIME type specified by - mimeType. If this object does not contain data for the- mimeTypeMIME type (see- hasFormat()), this function may perform a best effort conversion to it.- See also - 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 - hasText(),- hasHtml(),- hasUrls(),- hasImage(), and- hasColor()instead.- See also - hasColor()¶
- Return type:
- bool 
 
 - Returns - trueif the object can return a color (MIME type- application/x-color); otherwise returns- false.- See also - hasFormat(mimetype)¶
- Parameters:
- mimetype – str 
- Return type:
- bool 
 
 - Returns - trueif 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 - hasText(),- hasHtml(),- hasUrls(),- hasImage(), and- hasColor()instead.- hasHtml()¶
- Return type:
- bool 
 
 - Returns - trueif the object can return HTML (MIME type- text/html); otherwise returns- false.- See also - hasImage()¶
- Return type:
- bool 
 
 - Returns - trueif the object can return an image; otherwise returns false.- See also - hasText()¶
- Return type:
- bool 
 
 - Returns - trueif the object can return plain text (MIME type- text/plain); otherwise returns- false.- See also - hasUrls()¶
- Return type:
- bool 
 
 - Returns - trueif the object can return a list of urls; otherwise returns- false.- URLs correspond to the MIME type - text/uri-list.- See also - html()¶
- Return type:
- str 
 
 - Returns a string if the data stored in the object is HTML (MIME type - text/html); otherwise returns an empty string.- imageData()¶
- Return type:
- object 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Returns a - QVariantstoring a QImage if the object can return an image; otherwise returns a null variant.- A - QVariantis used because- QMimeDatabelongs to the Qt Core module, whereas QImage belongs to Qt GUI. To convert the- QVariantto a QImage, simply use- qvariant_cast(). For example:- if event.mimeData().hasImage(): image = QImage(event.mimeData().imageData()) ... - See also - removeFormat(mimetype)¶
- Parameters:
- mimetype – str 
 
 - Removes the data entry for - mimeTypein the object.- retrieveData(mimetype, preferredType)¶
- Parameters:
- mimetype – str 
- preferredType – - QMetaType
 
- Return type:
- object 
 
 - Returns a variant with the given - typecontaining data for the MIME type specified by- mimeType. If the object does not support the MIME type or variant type given, a null variant is returned instead.- This function is called by the general - data()getter and by the convenience getters (- text(),- html(),- urls(),- imageData(), and- colorData()). You can reimplement it if you want to store your data using a custom data structure (instead of a- QByteArray, which is what- setData()provides). You would then also need to reimplement- hasFormat()and- formats().- See also - 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.- See also - setData(mimetype, data)¶
- Parameters:
- mimetype – str 
- data – - QByteArray
 
 
 - Sets the data associated with the MIME type given by - mimeTypeto the specified- data.- For the most common types of data, you can call the higher-level functions - setText(),- setHtml(),- setUrls(),- setImageData(), and- 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.- See also - data()- hasFormat()- QMetaType- Q_DECLARE_METATYPE()- setHtml(html)¶
- Parameters:
- html – str 
 
 - Sets - htmlas the HTML (MIME type- text/html) used to represent the data.- setImageData(image)¶
- Parameters:
- image – object 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Sets the data in the object to the given - image.- A - QVariantis used because- QMimeDatabelongs to the Qt Core module, whereas QImage belongs to Qt GUI. The conversion from QImage to- QVariantis implicit. For example:- mimeData.setImageData(QImage("beautifulfjord.png")) - See also - setText(text)¶
- Parameters:
- text – str 
 
 - Sets - textas the plain text (MIME type- text/plain) used to represent the data.- setUrls(urls)¶
- Parameters:
- urls – .list of QUrl 
 
 - Sets the URLs stored in the MIME data object to those specified by - urls.- URLs correspond to the MIME type - text/uri-list.- Since Qt 5.0, setUrls also exports the urls as plain text, if - setTextwas not called before, to make it possible to drop them into any lineedit and text editor.- text()¶
- Return type:
- str 
 
 - Returns the plain text (MIME type - text/plain) representation of the data if this object contains plain text. If it contains some other content, this function makes a best effort to convert it to plain text.- Returns a list of URLs contained within the MIME data object. - URLs correspond to the MIME type - text/uri-list.