QtBase
v6.3.1
|
The QMimeData class provides a container for data that records information about its MIME type. More...
#include <qmimedata.h>
Protected Member Functions | |
virtual QVariant | retrieveData (const QString &mimetype, QMetaType preferredType) const |
Protected Member Functions inherited from QObject | |
QObject * | sender () const |
int | senderSignalIndex () const |
int | receivers (const char *signal) const |
bool | isSignalConnected (const QMetaMethod &signal) const |
virtual void | timerEvent (QTimerEvent *event) |
virtual void | childEvent (QChildEvent *event) |
virtual void | customEvent (QEvent *event) |
virtual void | connectNotify (const QMetaMethod &signal) |
virtual void | disconnectNotify (const QMetaMethod &signal) |
QObject (QObjectPrivate &dd, QObject *parent=nullptr) | |
The QMimeData class provides a container for data that records information about its MIME type.
\inmodule QtCore
QMimeData is used to describe information that can be stored in the \l{QClipboard}{clipboard}, and transferred via the \l{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:
\table \header
text/plain
\row text/html
\row text/uri-list
\row image/
* \row application/x-color
\endtableFor example, if your write a widget that accepts URL drags, you would end up writing code like this:
There are three approaches for storing custom data in a QMimeData object:
\list 1
\endlist
Definition at line 51 of file qmimedata.h.
QMimeData::QMimeData | ( | ) |
Constructs a new MIME data object with no data in it.
Definition at line 331 of file qmimedata.cpp.
QMimeData::~QMimeData | ( | ) |
Destroys the MIME data object.
Definition at line 339 of file qmimedata.cpp.
void QMimeData::clear | ( | ) |
Removes all the MIME type and data entries in the object.
Definition at line 677 of file qmimedata.cpp.
QVariant QMimeData::colorData | ( | ) | const |
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 because QMimeData belongs to the Qt Core module, whereas QColor belongs to Qt GUI. To convert the QVariant to a QColor, simply use qvariant_cast(). For example:
Definition at line 539 of file qmimedata.cpp.
QByteArray QMimeData::data | ( | const QString & | mimeType | ) | const |
Returns the data stored in the object in the format described by the MIME type specified by mimeType.
Definition at line 574 of file qmimedata.cpp.
|
virtual |
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.
Reimplemented in QTextEditMimeData, QQnxClipboard::MimeData, QIOSMimeData, QInternalMimeData, and QMacPasteboardMimeSource.
Definition at line 642 of file qmimedata.cpp.
bool QMimeData::hasColor | ( | ) | const |
Returns true
if the object can return a color (MIME type application/x-color
); otherwise returns false
.
Definition at line 565 of file qmimedata.cpp.
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 hasText(), hasHtml(), hasUrls(), hasImage(), and hasColor() instead.
Reimplemented in QQnxClipboard::MimeData, and QInternalMimeData.
Definition at line 626 of file qmimedata.cpp.
bool QMimeData::hasHtml | ( | ) | const |
Returns true
if the object can return HTML (MIME type text/html
); otherwise returns false
.
Definition at line 475 of file qmimedata.cpp.
bool QMimeData::hasImage | ( | ) | const |
Returns true
if the object can return an image; otherwise returns false.
Definition at line 521 of file qmimedata.cpp.
bool QMimeData::hasText | ( | ) | const |
Returns true
if the object can return plain text (MIME type text/plain
); otherwise returns false
.
Definition at line 439 of file qmimedata.cpp.
bool QMimeData::hasUrls | ( | ) | const |
Returns true
if the object can return a list of urls; otherwise returns false
.
URLs correspond to the MIME type text/uri-list
.
Definition at line 398 of file qmimedata.cpp.
QString QMimeData::html | ( | ) | const |
Returns a string if the data stored in the object is HTML (MIME type text/html
); otherwise returns an empty string.
Definition at line 450 of file qmimedata.cpp.
QVariant QMimeData::imageData | ( | ) | const |
Returns a QVariant storing a QImage if the object can return an image; otherwise returns a null variant.
A QVariant is used because QMimeData belongs to the Qt Core module, whereas QImage belongs to Qt GUI. To convert the QVariant to a QImage, simply use qvariant_cast(). For example:
Definition at line 492 of file qmimedata.cpp.
Removes the data entry for mimeType in the object.
Definition at line 688 of file qmimedata.cpp.
|
protectedvirtual |
Returns a variant with the given type containing 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().
Reimplemented in QTextEditMimeData, QIOSMimeData, QInternalMimeData, QQnxClipboard::MimeData, and QMacPasteboardMimeSource.
Definition at line 667 of file qmimedata.cpp.
Sets the color data in the object to the given color.
Colors correspond to the MIME type application/x-color
.
Definition at line 552 of file qmimedata.cpp.
void QMimeData::setData | ( | const QString & | mimeType, |
const QByteArray & | data | ||
) |
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 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 \l{QMetaType}{meta type}, using the Q_DECLARE_METATYPE() macro, and implement stream operators for it.
Definition at line 595 of file qmimedata.cpp.
Sets html as the HTML (MIME type text/html
) used to represent the data.
Definition at line 463 of file qmimedata.cpp.
Sets the data in the object to the given image.
A QVariant is used because QMimeData belongs to the Qt Core module, whereas QImage belongs to Qt GUI. The conversion from QImage to QVariant is implicit. For example:
Definition at line 509 of file qmimedata.cpp.
Sets text as the plain text (MIME type text/plain
) used to represent the data.
Definition at line 427 of file qmimedata.cpp.
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 setText was not called before, to make it possible to drop them into any lineedit and text editor.
Definition at line 378 of file qmimedata.cpp.
QString QMimeData::text | ( | ) | const |
Returns a plain text (MIME type text/plain
) representation of the data.
Definition at line 410 of file qmimedata.cpp.
Returns a list of URLs contained within the MIME data object.
URLs correspond to the MIME type text/uri-list
.
Definition at line 350 of file qmimedata.cpp.