|  | Home · All Namespaces · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# Delayed Encoding Example

Files:

- draganddrop/delayedencoding/mimedata.cpp
- draganddrop/delayedencoding/mimedata.h
- draganddrop/delayedencoding/sourcewidget.cpp
- draganddrop/delayedencoding/sourcewidget.h
- draganddrop/delayedencoding/images/example.svg
- draganddrop/delayedencoding/main.cpp
- draganddrop/delayedencoding/delayedencoding.pro
- draganddrop/delayedencoding/delayedencoding.qrc

The Delayed Encoding example shows how to delay preparing of data for drag and drop operations until a drop target is found.

The Export push button is pressed down to start the drag. The data for the drag and drop operation is not processed until the user of the application has found a valid drop target. This removes redundant processing if the operation is aborted. In our case, we have an SVG image that we wish to send as the `"image/png"` MIME type. It is the conversion from SVG to PNG we wish to delay - it can be quite expensive.

The example is implemented in two classes: `SourceWidget` and `MimeData`. The `SourceWidget` class sets up the GUI and starts the drag operation on request. The `MimeData` class, which inherits QMimeData, sends a signal when a drop target is found. This signal is connected to a slot in `SourceWidget`, which does the conversion from SVG to PNG.

## SourceWidget Class Definition

The `SourceWidget` class starts drag and drop operations and also does the image conversion.

     public slots:
         void createData(const QString &mimetype);
         void startDrag();

     private:
         QByteArray imageData;
         QSvgWidget *imageLabel;
         MimeData *mimeData;

The Export push button is connected to the `startDrag()` slot. The `createData()` slot will be invoked when data for the drag and drop operation is to be created.

## SourceWidget Class Implementation

Let's start our code tour with a look at the `startDrag()` slot.

     void SourceWidget::startDrag()
     {
         mimeData = new MimeData;

         connect(mimeData, SIGNAL(dataRequested(const QString &)),
                 this, SLOT(createData(const QString &)), Qt::DirectConnection);

         QDrag *drag = new QDrag(this);
         drag->setMimeData(mimeData);
         drag->setPixmap(QPixmap(":/images/drag.png"));

         drag->exec(Qt::CopyAction);
     }

We emit `dataRequested()` from `MimeData` when the operation has found a valid drop target.

We gallop along to `createData()`:

     void SourceWidget::createData(const QString &mimeType)
     {
         if (mimeType != "image/png")
             return;

         QImage image(imageLabel->size(), QImage::Format_RGB32);
         QPainter painter;
         painter.begin(&image);
         imageLabel->renderer()->render(&painter);
         painter.end();

         QByteArray data;
         QBuffer buffer(&data);
         buffer.open(QIODevice::WriteOnly);
         image.save(&buffer, "PNG");
         buffer.close();

         mimeData->setData("image/png", data);
     }

Fortunately, Qt provides QSvgRenderer, which can render the SVG image to any QPaintDevice. Also, QImage has no problems saving to the PNG format.

Finally, we can give the data to `MimeData`.

## MimeData Class Definition

The `MimeData` class inherits QMimeData and makes it possible to delay preparing of the data for a drag and drop operation.

     class MimeData : public QMimeData
     {
         Q_OBJECT

     public:
         MimeData();
         QStringList formats() const;

     signals:
         void dataRequested(const QString &mimeType) const;

     protected:
         QVariant retrieveData(const QString &mimetype, QVariant::Type type) const;
     };

We will look closer at `retrieveData()` and `formats()` in the next section.

## MimeData Class Implementation

     QStringList MimeData::formats() const
     {
         return QMimeData::formats() << "image/png";
     }

In the `formats()` function, we return the format of the data we provide. This is the `image/png` MIME type.

     QVariant MimeData::retrieveData(const QString &mimeType, QVariant::Type type)
              const
     {
         emit dataRequested(mimeType);

         return QMimeData::retrieveData(mimeType, type);
     }

`retrieveData()` is reimplemented from QMimeData and is called when the data is requested by the drag and drop operation. Fortunately for us, this happens when the operation is finishing, i.e., when a drop target has been found.

We emit the `dataRequested()` signal, which is picked up by `SourceWidget`. The `SourceWidget` (as already explained) sets the data on `MimeData` with setData().