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
。
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.
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()
槽。
createData()
slot will be invoked when data for the drag and drop operation is to be created.
Let's start our code tour with a look at the
startDrag()
槽。
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
.
The
MimeData
类继承
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.
QStringList MimeData::formats() const { return QMimeData::formats() << "image/png"; }
在
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
。
SourceWidget
(as already explained) sets the data on
MimeData
with
setData()
.
文件:
图像: