QMimeData 類

The QMimeData 類為記錄其 MIME (多用途 Internet 郵件擴展) 類型有關信息的數據提供容器。 更多...

頭: #include <QMimeData>
qmake: QT += core
繼承: QObject

公共函數

QMimeData ()
~QMimeData ()
void clear ()
QVariant colorData () const
QByteArray data (const QString & mimeType ) const
virtual QStringList formats () 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 , QVariant::Type type ) const

額外繼承成員

詳細描述

The QMimeData 類為記錄其 MIME (多用途 Internet 郵件擴展) 類型有關信息的數據提供容器。

QMimeData 用來描述的信息可以存儲在 clipboard ,和轉移憑藉 拖放 機製。 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 且供給 QDrag or QClipboard 對象。這使 Qt 能夠管理它們所使用的內存。

A single QMimeData object can store the same data using several different formats at the same time. The formats () 函數按首選次序排列,返迴可用格式列錶。 data () 函數返迴關聯 MIME 類型的原生數據,和 setData () 允許為 MIME 類型設置數據。

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

例如,若編寫接受 URL 拖拽的 Widget,最終將寫齣像這樣的代碼:

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 對象:

  1. Custom data can be stored directly in a QMimeData object as a QByteArray 使用 setData ()。例如:
    QByteArray csvData = ...;
    QMimeData *mimeData = new QMimeData;
    mimeData->setData("text/csv", csvData);
    							
  2. 可以子類化 QMimeData 並重實現 hasFormat (), formats (),和 retrieveData ().
  3. If the drag and drop operation occurs within a single application, we can subclass QMimeData and add extra data in it, and use a qobject_cast () 在接收者的掉落事件處理程序中。例如:
    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)
        }
    }
    							

特定平颱 MIME 類型

在 Windows, formats () 還會返迴可用於 MIME (多用途 Internet 郵件擴展) 數據的自定義格式,使用 x-qt-windows-mime 子類型以指示它們以非標格式錶示數據。​格式將接受以下形式:

application/x-qt-windows-mime;value="<custom type>"
					

以下是自定義 MIME (多用途 Internet 郵件擴展) 類型範例:

application/x-qt-windows-mime;value="FileGroupDescriptor"
application/x-qt-windows-mime;value="FileContents"
					

The value 聲明的每種格式描述瞭數據編碼的具體方式。

在某些情況下 (如:掉落多個 Email 附件),多個數據值是可用的。可以訪問它們通過添加 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 QWinMime to map clipboard formats to open-standard MIME formats. Similarly, the QMacPasteboardMime maps MIME to Mac flavors.

另請參閱 QClipboard , QDragEnterEvent , QDragMoveEvent , QDropEvent , QDrag , QMacPasteboardMime ,和 拖放 .

成員函數文檔編製

QMimeData:: QMimeData ()

構造其中沒有數據的新 MIME (多用途 Internet 郵件擴展) 數據對象。

QMimeData:: ~QMimeData ()

銷毀 MIME (多用途 Internet 郵件擴展) 數據對象。

void QMimeData:: clear ()

移除對象中的所有 MIME 類型和數據條目。

QVariant QMimeData:: colorData () const

返迴顔色若存儲在對象中的數據錶示顔色 (MIME 類型 application/x-color );否則返迴 null 變體。

A QVariant 被使用因為 QMimeData 屬於 Qt Core 模塊,而 QColor 屬於 Qt GUI。要轉換 QVariant QColor ,隻需使用 qvariant_cast ()。例如:

if (event->mimeData()->hasColor()) {
    QColor color = qvariant_cast<QColor>(event->mimeData()->colorData());
    ...
}
					

另請參閱 hasColor (), setColorData (),和 data ().

QByteArray QMimeData:: data (const QString & mimeType ) const

返迴存儲在對象中的數據,按 MIME (多用途 Internet 郵件擴展) 類型描述的格式指定通過 mimeType .

另請參閱 setData ().

[虛擬] QStringList QMimeData:: formats () const

返迴對象支持的格式列錶。這是對象可以返迴閤適數據的 MIME 類型列錶。列錶中的格式按優先級次序排列。

對於最常見數據類型,可以調用更高級函數 hasText (), hasHtml (), hasUrls (), hasImage (),和 hasColor () 代替。

另請參閱 hasFormat (), setData (),和 data ().

bool QMimeData:: hasColor () const

返迴 true 若對象可以返迴顔色 (MIME 類型 application/x-color );否則返迴 false .

另請參閱 setColorData (), colorData (),和 hasFormat ().

[虛擬] bool QMimeData:: hasFormat (const QString & mimeType ) const

返迴 true 若對象可以返迴 MIME (多用途 Internet 郵件擴展) 類型的數據,指定通過 mimeType ;否則返迴 false .

對於最常見數據類型,可以調用更高級函數 hasText (), hasHtml (), hasUrls (), hasImage (),和 hasColor () 代替。

另請參閱 formats (), setData (),和 data ().

bool QMimeData:: hasHtml () const

返迴 true 若對象可以返迴 HTML (MIME 類型 text/html );否則返迴 false .

另請參閱 setHtml (), html (),和 hasFormat ().

bool QMimeData:: hasImage () const

返迴 true 若對象可以返迴圖像;否則返迴 false。

另請參閱 setImageData (), imageData (),和 hasFormat ().

bool QMimeData:: hasText () const

返迴 true 若對象可以返迴純文本 (MIME 類型 text/plain );否則返迴 false .

另請參閱 setText (), text (), hasHtml (),和 hasFormat ().

bool QMimeData:: hasUrls () const

返迴 true 若對象可以返迴 URL 列錶;否則返迴 false .

URL 對應於 MIME 類型 text/uri-list .

另請參閱 setUrls (), urls (),和 hasFormat ().

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.

另請參閱 setHtml (), hasHtml (),和 setData ().

QVariant QMimeData:: imageData () const

返迴 QVariant storing a QImage if the object can return an image; otherwise returns a null variant.

A QVariant 被使用因為 QMimeData 屬於 Qt Core 模塊,而 QImage 屬於 Qt GUI。要轉換 QVariant QImage ,隻需使用 qvariant_cast ()。例如:

if (event->mimeData()->hasImage()) {
    QImage image = qvariant_cast<QImage>(event->mimeData()->imageData());
    ...
}
					

另請參閱 setImageData () 和 hasImage ().

void QMimeData:: removeFormat (const QString & mimeType )

Removes the data entry for mimeType in the object.

該函數在 Qt 4.4 引入。

[virtual protected] QVariant QMimeData:: retrieveData (const QString & mimeType , QVariant::Type 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 () 和 formats ().

另請參閱 data ().

void QMimeData:: setColorData (const QVariant & color )

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

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

另請參閱 colorData (), hasColor (),和 setData ().

void QMimeData:: setData (const QString & mimeType , const QByteArray & data )

Sets the data associated with the MIME type given by mimeType 到指定 data .

對於最常見數據類型,可以調用更高級函數 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. The stream operators must then be registered with the qRegisterMetaTypeStreamOperators () 函數。

另請參閱 data (), hasFormat (), QMetaType ,和 qRegisterMetaTypeStreamOperators ().

void QMimeData:: setHtml (const QString & html )

html 作為 HTML (MIME 類型 text/html ) 用於錶示數據。

另請參閱 html (), hasHtml (), setText (),和 setData ().

void QMimeData:: setImageData (const QVariant & image )

將對象中的數據設為給定 image .

A QVariant 被使用因為 QMimeData 屬於 Qt Core 模塊,而 QImage belongs to Qt GUI. The conversion from QImage to QVariant is implicit. For example:

mimeData->setImageData(QImage("beautifulfjord.png"));
					

另請參閱 imageData (), hasImage (),和 setData ().

void QMimeData:: setText (const QString & text )

text 作為純文本 (MIME 類型 text/plain ) 用於錶示數據。

另請參閱 text (), hasText (), setHtml (),和 setData ().

void QMimeData:: setUrls (const QList < QUrl > & urls )

Sets the URLs stored in the MIME data object to those specified by urls .

URL 對應於 MIME 類型 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 ().

QString QMimeData:: text () const

返迴純文本 (MIME 類型 text/plain ) 錶示的數據。

另請參閱 setText (), hasText (), html (),和 data ().

QList < QUrl > QMimeData:: urls () const

返迴 MIME (多用途 Internet 郵件擴展) 數據對象中包含的 URL 列錶。

URL 對應於 MIME 類型 text/uri-list .

另請參閱 setUrls (), hasUrls (),和 data ().