QMimeData¶
Synopsis¶
Functions¶
def
clear
()def
colorData
()def
data
(mimetype)def
hasColor
()def
hasHtml
()def
hasImage
()def
hasText
()def
hasUrls
()def
html
()def
imageData
()def
removeFormat
(mimetype)def
setColorData
(color)def
setData
(mimetype, data)def
setHtml
(html)def
setImageData
(image)def
setText
(text)def
setUrls
(urls)def
text
()def
urls
()
Virtual functions¶
def
formats
()def
hasFormat
(mimetype)def
retrieveData
(mimetype, preferredType)
Detailed Description¶
QMimeData
is used to describe information that can be stored in theclipboard
, 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 usingnew
and supplied toQDrag
orQClipboard
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. Theformats()
function returns a list of the available formats in order of preference. Thedata()
function returns the raw data associated with a MIME type, andsetData()
allows you to set the data for a MIME type.For the most common MIME types,
QMimeData
provides 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
QMimeData
object:
Custom data can be stored directly in a
QMimeData
object as aQByteArray
usingsetData()
. For example:csvData = QByteArray(...) mimeData = QMimeData() mimeData.setData("text/csv", csvData)We can subclass
QMimeData
and reimplementhasFormat()
,formats()
, andretrieveData()
.If the drag and drop operation occurs within a single application, we can subclass
QMimeData
and add extra data in it, and use aqobject_cast()
in the receiver’s drop event handler. For example:def dropEvent(self, event): myData = 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 thex-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.In some cases (e.g. dropping multiple email attachments), multiple data values are available. They can be accessed by adding an
index
value:application/x-qt-windows-mime;value="FileContents";index=0 application/x-qt-windows-mime;value="FileContents";index=1On Windows, the MIME format does not always map directly to the clipboard formats. Qt provides QWinMime to map clipboard formats to open-standard MIME formats. Similarly, the QMacPasteboardMime maps MIME to Mac flavors.
See also
QClipboard
QDragEnterEvent
QDragMoveEvent
QDropEvent
QDrag
Drag and Drop
- class PySide2.QtCore.QMimeData¶
Constructs a new MIME data object with no data in it.
- PySide2.QtCore.QMimeData.clear()¶
Removes all the MIME type and data entries in the object.
- PySide2.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
QVariant
is used becauseQMimeData
belongs to the Qt Core module, whereasQColor
belongs to Qt GUI. To convert theQVariant
to aQColor
, simply useqvariant_cast()
. For example:if event.mimeData().hasColor(): color = QColor(event.mimeData().colorData()) ...
See also
- PySide2.QtCore.QMimeData.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
.See also
- PySide2.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
hasText()
,hasHtml()
,hasUrls()
,hasImage()
, andhasColor()
instead.See also
- PySide2.QtCore.QMimeData.hasColor()¶
- Return type:
bool
Returns
true
if the object can return a color (MIME typeapplication/x-color
); otherwise returnsfalse
.See also
- PySide2.QtCore.QMimeData.hasFormat(mimetype)¶
- Parameters:
mimetype – str
- Return type:
bool
Returns
true
if the object can return data for the MIME type specified bymimeType
; otherwise returnsfalse
.For the most common types of data, you can call the higher-level functions
hasText()
,hasHtml()
,hasUrls()
,hasImage()
, andhasColor()
instead.
- PySide2.QtCore.QMimeData.hasHtml()¶
- Return type:
bool
Returns
true
if the object can return HTML (MIME typetext/html
); otherwise returnsfalse
.See also
- PySide2.QtCore.QMimeData.hasImage()¶
- Return type:
bool
Returns
true
if the object can return an image; otherwise returns false.See also
- PySide2.QtCore.QMimeData.hasText()¶
- Return type:
bool
Returns
true
if the object can return plain text (MIME typetext/plain
); otherwise returnsfalse
.See also
- PySide2.QtCore.QMimeData.hasUrls()¶
- Return type:
bool
Returns
true
if the object can return a list of urls; otherwise returnsfalse
.URLs correspond to the MIME type
text/uri-list
.See also
- PySide2.QtCore.QMimeData.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.
- PySide2.QtCore.QMimeData.imageData()¶
- Return type:
object
Returns a
QVariant
storing aQImage
if the object can return an image; otherwise returns a null variant.A
QVariant
is used becauseQMimeData
belongs to the Qt Core module, whereasQImage
belongs to Qt GUI. To convert theQVariant
to aQImage
, simply useqvariant_cast()
. For example:if event.mimeData().hasImage(): image = QImage(event.mimeData().imageData()) ...
See also
- PySide2.QtCore.QMimeData.removeFormat(mimetype)¶
- Parameters:
mimetype – str
Removes the data entry for
mimeType
in the object.
- PySide2.QtCore.QMimeData.retrieveData(mimetype, preferredType)¶
- Parameters:
mimetype – str
preferredType –
QVariant::Type
- Return type:
object
Returns a variant with the given
type
containing data for the MIME type specified bymimeType
. 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()
, andcolorData()
). You can reimplement it if you want to store your data using a custom data structure (instead of aQByteArray
, which is whatsetData()
provides). You would then also need to reimplementhasFormat()
andformats()
.See also
- PySide2.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
.See also
- PySide2.QtCore.QMimeData.setData(mimetype, data)¶
- Parameters:
mimetype – str
data –
PySide2.QtCore.QByteArray
Sets the data associated with the MIME type given by
mimeType
to the specifieddata
.For the most common types of data, you can call the higher-level functions
setText()
,setHtml()
,setUrls()
,setImageData()
, andsetColorData()
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 theQ_DECLARE_METATYPE()
macro, and implement stream operators for it. The stream operators must then be registered with theqRegisterMetaTypeStreamOperators()
function.See also
data()
hasFormat()
QMetaType
qRegisterMetaTypeStreamOperators()
- PySide2.QtCore.QMimeData.setHtml(html)¶
- Parameters:
html – str
Sets
html
as the HTML (MIME typetext/html
) used to represent the data.
- PySide2.QtCore.QMimeData.setImageData(image)¶
- Parameters:
image – object
Sets the data in the object to the given
image
.A
QVariant
is used becauseQMimeData
belongs to the Qt Core module, whereasQImage
belongs to Qt GUI. The conversion fromQImage
toQVariant
is implicit. For example:mimeData.setImageData(QImage("beautifulfjord.png"))
See also
- PySide2.QtCore.QMimeData.setText(text)¶
- Parameters:
text – str
Sets
text
as the plain text (MIME typetext/plain
) used to represent the data.
- PySide2.QtCore.QMimeData.setUrls(urls)¶
- Parameters:
urls –
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, also exports the urls as plain text, if
setText
was not called before, to make it possible to drop them into any lineedit and text editor.
- PySide2.QtCore.QMimeData.text()¶
- Return type:
str
Returns a plain text (MIME type
text/plain
) representation of the data.
- PySide2.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
.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.