Home · All Classes · All Functions · Overviews

Delayed Encoding Example



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();

     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(QString)),
             this, SLOT(createData(QString)), Qt::DirectConnection);

     QDrag *drag = new QDrag(this);


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")

     QImage image(imageLabel->size(), QImage::Format_RGB32);
     QPainter painter;

     QByteArray data;
     QBuffer buffer(&data);
     image.save(&buffer, "PNG");

     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

     QStringList formats() const;

     void dataRequested(const QString &mimeType) const;

     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)
     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().

Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies) Trademarks
Qt 4.6.3