QMimeData 类为记录其 MIME (多用途 Internet 邮件扩展) 类型有关信息的数据提供容器。 更多...
头: | #include <QMimeData> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
继承: | QObject |
QMimeData () | |
virtual | ~QMimeData () |
void | clear () |
QVariant | colorData () const |
QByteArray | data (const QString & mimeType ) const |
virtual QStringList | 格式 () const |
bool | hasColor () const |
virtual bool | hasFormat (const QString & mimeType ) const |
bool | hasHtml () const |
bool | hasImage () const |
bool | hasText () const |
bool | hasUrls () const |
QString | html () const |
QVariant | imageData () const |
void | removeFormat (const QString & mimeType ) |
void | setColorData (const QVariant & color ) |
void | setData (const QString & mimeType , const QByteArray & data ) |
void | setHtml (const QString & html ) |
void | setImageData (const QVariant & image ) |
void | setText (const QString & text ) |
void | setUrls (const QList<QUrl> & urls ) |
QString | text () const |
QList<QUrl> | urls () const |
virtual QVariant | retrieveData (const QString & mimeType , QMetaType type ) const |
QMimeData is used to describe information that can be stored in the clipboard ,和传输凭借 拖放 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 格式 () 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:
Tester | Getter | Setter | MIME (多用途 Internet 邮件扩展) 类型 |
---|---|---|---|
hasText () | text () | setText () |
text/plain
|
hasHtml () | html () | setHtml () |
text/html
|
hasUrls () | urls () | setUrls () |
text/uri-list
|
hasImage () | imageData () | setImageData () |
image/
*
|
hasColor () | colorData () | setColorData () |
application/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()) { foreach (QUrl url, event->mimeData()->urls()) { ... } } }
There are three approaches for storing custom data in a QMimeData object:
QByteArray csvData = ...; QMimeData *mimeData = new QMimeData; mimeData->setData("text/csv", csvData);
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) } }
在 Windows,
格式
() 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.
In some cases (e.g. dropping multiple email attachments), multiple data values are available. They can be accessed by adding an
index
值:
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.
另请参阅 QClipboard , QDragEnterEvent , QDragMoveEvent , QDropEvent , QDrag ,和 拖放 .
构造其中没有数据的新 MIME (多用途 Internet 邮件扩展) 数据对象。
[virtual noexcept]
QMimeData::
~QMimeData
()
销毁 MIME (多用途 Internet 邮件扩展) 数据对象。
移除对象中的所有 MIME 类型和数据条目。
返回颜色若存储在对象中的数据表示颜色 (MIME 类型
application/x-color
);否则返回 null 变体。
A QVariant is used because QMimeData belongs to the Qt Core module, whereas QColor belongs to Qt GUI. To convert the QVariant 到 QColor , simply use qvariant_cast ()。例如:
if (event->mimeData()->hasColor()) { QColor color = qvariant_cast<QColor>(event->mimeData()->colorData()); ... }
另请参阅 hasColor (), setColorData (),和 data ().
Returns the data stored in the object in the format described by the MIME type specified by mimeType .
另请参阅 setData ().
[virtual]
QStringList
QMimeData::
格式
() const
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 (),和 hasColor () 代替。
另请参阅 hasFormat (), setData (),和 data ().
返回
true
若对象可以返回颜色 (MIME 类型
application/x-color
);否则返回
false
.
另请参阅 setColorData (), colorData (),和 hasFormat ().
[virtual]
bool
QMimeData::
hasFormat
(const
QString
&
mimeType
) const
返回
true
if the object can return data for the MIME type specified by
mimeType
;否则返回
false
.
For the most common types of data, you can call the higher-level functions hasText (), hasHtml (), hasUrls (), hasImage (),和 hasColor () 代替。
另请参阅 格式 (), setData (),和 data ().
返回
true
若对象可以返回 HTML (MIME 类型
text/html
);否则返回
false
.
另请参阅 setHtml (), html (),和 hasFormat ().
返回
true
若对象可以返回图像;否则返回 false。
另请参阅 setImageData (), imageData (),和 hasFormat ().
返回
true
若对象可以返回纯文本 (MIME 类型
text/plain
);否则返回
false
.
另请参阅 setText (), text (), hasHtml (),和 hasFormat ().
返回
true
if the object can return a list of urls; otherwise returns
false
.
URLs correspond to the MIME type
text/uri-list
.
另请参阅 setUrls (), urls (),和 hasFormat ().
Returns a string if the data stored in the object is HTML (MIME type
text/html
); otherwise returns an empty string.
另请参阅 setHtml (), hasHtml (),和 setData ().
返回 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 到 QImage , simply use qvariant_cast ()。例如:
if (event->mimeData()->hasImage()) { QImage image = qvariant_cast<QImage>(event->mimeData()->imageData()); ... }
另请参阅 setImageData () 和 hasImage ().
Removes the data entry for mimeType in the object.
[virtual protected]
QVariant
QMimeData::
retrieveData
(const
QString
&
mimeType
,
QMetaType
type
) const
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 (),和 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 () 和 格式 ().
另请参阅 data ().
Sets the color data in the object to the given color .
Colors correspond to the MIME type
application/x-color
.
另请参阅 colorData (), hasColor (),和 setData ().
Sets the data associated with the MIME type given by mimeType 到指定 data .
For the most common types of data, you can call the higher-level functions setText (), setHtml (), setUrls (), setImageData (),和 setColorData () 代替。
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 ,使用 Q_DECLARE_METATYPE () macro, and implement stream operators for it.
另请参阅 data (), hasFormat (), QMetaType ,和 Q_DECLARE_METATYPE ().
设置
html
as the HTML (MIME type
text/html
) used to represent the data.
另请参阅 html (), hasHtml (), setText (),和 setData ().
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:
mimeData->setImageData(QImage("beautifulfjord.png"));
另请参阅 imageData (), hasImage (),和 setData ().
设置
text
as the plain text (MIME type
text/plain
) used to represent the data.
另请参阅 text (), hasText (), setHtml (),和 setData ().
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.
另请参阅 urls (), hasUrls (),和 setData ().
Returns a plain text (MIME type
text/plain
) representation of the data.
另请参阅 setText (), hasText (), html (),和 data ().
Returns a list of URLs contained within the MIME data object.
URLs correspond to the MIME type
text/uri-list
.