From 1750acc3921148b0ffc8f9f1421b87a2f5522a2e Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Thu, 13 Mar 2025 16:53:50 +0100 Subject: [PATCH] DropSite example: handle application/x-color Show the color that is dragged in, both graphically and textually. Revert to default palette for other data types. Prioritize colors over text: if an application (such as kolourpaint) offers both x-color and text, the color is probably the intention, and the text may be just a hex string for placing into a color text field. Pick-to: 6.8 6.9 Fixes: QTBUG-134313 Change-Id: I6c19f1220712881d2057bc9758bbc8cbd9247c81 Reviewed-by: Liang Qi --- examples/widgets/draganddrop/dropsite/droparea.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/examples/widgets/draganddrop/dropsite/droparea.cpp b/examples/widgets/draganddrop/dropsite/droparea.cpp index 1b2ff1820d0..0dda206e9e7 100644 --- a/examples/widgets/draganddrop/dropsite/droparea.cpp +++ b/examples/widgets/draganddrop/dropsite/droparea.cpp @@ -48,6 +48,11 @@ void DropArea::dropEvent(QDropEvent *event) //! [dropEvent() function part2] if (mimeData->hasImage()) { setPixmap(qvariant_cast(mimeData->imageData())); + } else if (mimeData->hasColor()) { + const auto color = mimeData->colorData().value(); + setText(tr("Color: %1").arg(color.name())); + setPalette(QPalette(color)); + setBackgroundRole(QPalette::Button); } else if (mimeData->hasFormat(u"text/markdown"_s)) { setText(QString::fromUtf8(mimeData->data(u"text/markdown"_s))); setTextFormat(Qt::MarkdownText); @@ -69,7 +74,10 @@ void DropArea::dropEvent(QDropEvent *event) //! [dropEvent() function part2] //! [dropEvent() function part3] - setBackgroundRole(QPalette::Dark); + if (!mimeData->hasColor()) { + setPalette({}); + setBackgroundRole(QPalette::Dark); + } event->acceptProposedAction(); } //! [dropEvent() function part3]