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 <liang.qi@qt.io>
This commit is contained in:
Shawn Rutledge 2025-03-13 16:53:50 +01:00
parent 70b8dd2b7e
commit 1750acc392

View File

@ -48,6 +48,11 @@ void DropArea::dropEvent(QDropEvent *event)
//! [dropEvent() function part2]
if (mimeData->hasImage()) {
setPixmap(qvariant_cast<QPixmap>(mimeData->imageData()));
} else if (mimeData->hasColor()) {
const auto color = mimeData->colorData().value<QColor>();
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]