faff1df
From c257eb6a0eeb159c670c9cf6b37d3009ec8879e5 Mon Sep 17 00:00:00 2001
faff1df
From: Fushan Wen <qydwhotmail@gmail.com>
faff1df
Date: Tue, 1 Nov 2022 22:35:24 +0800
faff1df
Subject: [PATCH 07/19] Don't convert QByteArray in `startDrag`
faff1df
faff1df
QMimeData::setData expects the provided data to contain the correctly
faff1df
encoded QByteArray, so if the variant contains a QByteArray, then take
faff1df
it as is to avoid data loss.
faff1df
faff1df
If the variant is not already a byte array, then we ideally would make
faff1df
sure that the mime type (i.e. the key of the map) and the QVariant's
faff1df
type are compatible (image/png with a QImage works; text/plain with a
faff1df
QImage does not). This changes behavior and needs to be a follow-up
faff1df
commit.
faff1df
faff1df
Fixes: QTBUG-71922
faff1df
Change-Id: I9b9f10fd332e1f9568f6835a69a1c359457f823c
faff1df
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
faff1df
(cherry picked from commit 062f9bf57657b54dc708015ec5fed3c89e5cc3ca)
faff1df
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
faff1df
faff1df
faff1df
(cherry picked from commit 22de23c4bb9ac5e2c545e9de3149a7d4f8edd5ee)
faff1df
---
faff1df
 src/quick/items/qquickdrag.cpp | 12 +++++++++---
faff1df
 1 file changed, 9 insertions(+), 3 deletions(-)
faff1df
faff1df
diff --git a/src/quick/items/qquickdrag.cpp b/src/quick/items/qquickdrag.cpp
faff1df
index 8321fcfeed..3b50370355 100644
faff1df
--- a/src/quick/items/qquickdrag.cpp
faff1df
+++ b/src/quick/items/qquickdrag.cpp
faff1df
@@ -481,7 +481,9 @@ void QQuickDragAttached::setKeys(const QStringList &keys)
faff1df
     \qmlattachedproperty stringlist QtQuick::Drag::mimeData
faff1df
     \since 5.2
faff1df
 
faff1df
-    This property holds a map of mimeData that is used during startDrag.
faff1df
+    This property holds a map from mime type to data that is used during startDrag.
faff1df
+    The mime data needs to be a \c string, or an \c ArrayBuffer with the data encoded
faff1df
+    according to the mime type.
faff1df
 */
faff1df
 
faff1df
 QVariantMap QQuickDragAttached::mimeData() const
faff1df
@@ -766,8 +768,12 @@ Qt::DropAction QQuickDragAttachedPrivate::startDrag(Qt::DropActions supportedAct
faff1df
     QDrag *drag = new QDrag(source ? source : q);
faff1df
     QMimeData *mimeData = new QMimeData();
faff1df
 
faff1df
-    for (auto it = externalMimeData.cbegin(), end = externalMimeData.cend(); it != end; ++it)
faff1df
-        mimeData->setData(it.key(), it.value().toString().toUtf8());
faff1df
+    for (auto it = externalMimeData.cbegin(), end = externalMimeData.cend(); it != end; ++it) {
faff1df
+        if (it.value().typeId() == QMetaType::QByteArray)
faff1df
+            mimeData->setData(it.key(), it.value().toByteArray());
faff1df
+        else
faff1df
+            mimeData->setData(it.key(), it.value().toString().toUtf8());
faff1df
+    }
faff1df
 
faff1df
     drag->setMimeData(mimeData);
faff1df
     if (pixmapLoader.isReady()) {
faff1df
-- 
faff1df
2.40.0
faff1df