Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

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

 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(QString)),
             this, SLOT(createData(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().

Files:

Images:

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web