IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QMimeData Class

The https:// class provides a container for data that records information about its MIME type.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QMimeData Class▲

  • Header: QMimeData

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS Core)

    target_link_libraries(mytarget PRIVATE Qt6::Core)

  • qmake: QT += core

  • Inherits: https://

Detailed Description▲

https:// is used to describe information that can be stored in the clipboard, and transferred via the drag and drop mechanism. https:// 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.

https:// 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 https:// object can store the same data using several different formats at the same time. The https://() function returns a list of the available formats in order of preference. The https://() function returns the raw data associated with a MIME type, and https://() allows you to set the data for a MIME type.

For the most common MIME types, https:// provides convenience functions to access the data:

Tester

Getter

Setter

MIME Types

https://()

https://()

https://()

text/plain

https://()

https://()

https://()

text/html

https://()

https://()

https://()

text/uri-list

https://()

https://()

https://()

image/ *

https://()

https://()

https://()

application/x-color

For example, if your write a widget that accepts URL drags, you would end up writing code like this:

 
Sélectionnez
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 https:// object:

  1. Custom data can be stored directly in a https:// object as a https:// using https://(). For example:

     
    Sélectionnez
    QByteArray csvData = ...;
    
    QMimeData *mimeData = new QMimeData;
    mimeData->setData("text/csv", csvData);
  2. We can subclass https:// and reimplement https://(), https://(), and https://().

  3. If the drag and drop operation occurs within a single application, we can subclass https:// and add extra data in it, and use a https://() in the receiver's drop event handler. For example:

     
    Sélectionnez
    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)
        }
    }

Platform-Specific MIME Types▲

On Windows, https://() 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:

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

The following are examples of custom MIME types:

 
Sélectionnez
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:

 
Sélectionnez
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.

See Also▲

See also QClipboard, QDragEnterEvent, QDragMoveEvent, QDropEvent, QDrag, Drag and Drop

Member Function Documentation▲

 

QMimeData::QMimeData()▲

Constructs a new MIME data object with no data in it.

[virtual] QMimeData::~QMimeData()▲

Destroys the MIME data object.

void QMimeData::clear()▲

Removes all the MIME type and data entries in the object.

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 https:// is used because https:// belongs to the Qt Core module, whereas QColor belongs to Qt GUI. To convert the https:// to a QColor, simply use https://(). For example:

 
Sélectionnez
if (event->mimeData()->hasColor()) {
    QColor color = qvariant_cast<QColor>(event->mimeData()->colorData());
    ...
}
See Also▲

See also https://(), https://(), https://()

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.

See Also▲

See also https://()

[virtual] QStringList QMimeData::formats() 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 https://(), https://(), https://(), https://(), and https://() instead.

See Also▲

See also https://(), https://(), https://()

bool QMimeData::hasColor() const▲

Returns true if the object can return a color (MIME type application/x-color); otherwise returns false.

See Also▲

See also https://(), https://(), https://()

[virtual] bool QMimeData::hasFormat(const QString &mimeType) const▲

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 https://(), https://(), https://(), https://(), and https://() instead.

See Also▲

See also https://(), https://(), https://()

bool QMimeData::hasHtml() const▲

Returns true if the object can return HTML (MIME type text/html); otherwise returns false.

See Also▲

See also https://(), https://(), https://()

bool QMimeData::hasImage() const▲

Returns true if the object can return an image; otherwise returns false.

See Also▲

See also https://(), https://(), https://()

bool QMimeData::hasText() const▲

Returns true if the object can return plain text (MIME type text/plain); otherwise returns false.

See Also▲

See also https://(), https://(), https://(), https://()

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.

See Also▲

See also https://(), https://(), https://()

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.

See Also▲

See also https://(), https://(), https://()

QVariant QMimeData::imageData() const▲

Returns a https:// storing a QImage if the object can return an image; otherwise returns a null variant.

A https:// is used because https:// belongs to the Qt Core module, whereas QImage belongs to Qt GUI. To convert the https:// to a QImage, simply use https://(). For example:

 
Sélectionnez
if (event->mimeData()->hasImage()) {
    QImage image = qvariant_cast<QImage>(event->mimeData()->imageData());
    ...
}
See Also▲

See also https://(), https://()

void QMimeData::removeFormat(const QString &mimeType)▲

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 https://() getter and by the convenience getters (https://(), https://(), https://(), https://(), and https://()). You can reimplement it if you want to store your data using a custom data structure (instead of a https://, which is what https://() provides). You would then also need to reimplement https://() and https://().

See Also▲

See also https://()

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.

See Also▲

See also https://(), https://(), https://()

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 https://(), https://(), https://(), https://(), and https://() 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 https://, using the https://() macro, and implement stream operators for it.

See Also▲

See also https://(), https://(), https://, https://()

void QMimeData::setHtml(const QString &html)▲

Sets html as the HTML (MIME type text/html) used to represent the data.

See Also▲

See also https://(), https://(), https://(), https://()

void QMimeData::setImageData(const QVariant &image)▲

Sets the data in the object to the given image.

A https:// is used because https:// belongs to the Qt Core module, whereas QImage belongs to Qt GUI. The conversion from QImage to https:// is implicit. For example:

 
Sélectionnez
mimeData->setImageData(QImage("beautifulfjord.png"));
See Also▲

See also https://(), https://(), https://()

void QMimeData::setText(const QString &text)▲

Sets text as the plain text (MIME type text/plain) used to represent the data.

See Also▲

See also https://(), https://(), https://(), https://()

void QMimeData::setUrls(const QList<QUrl> &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, setUrls also exports the urls as plain text, if https:// was not called before, to make it possible to drop them into any lineedit and text editor.

See Also▲

See also https://(), https://(), https://()

QString QMimeData::text() const▲

Returns a plain text (MIME type text/plain) representation of the data.

See Also▲

See also https://(), https://(), https://(), https://()

QList<QUrl> QMimeData::urls() const▲

Returns a list of URLs contained within the MIME data object.

URLs correspond to the MIME type text/uri-list.

See Also▲

See also https://(), https://(), https://()

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Ce document est disponible sous la licence https:// telle que publiée par la Free Software Foundation.