[frameworks/kwidgetsaddons] src: KColorCombo: support d'n'dropping colors to set the color

Friedrich W. H. Kossebau <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 1078245d95a3cb9e9526b218273ad9264157b0f7 by Friedrich W. H. Kossebau.
Committed on 24/07/2026 at 21:11.
Pushed by kossebau into branch 'master'.

KColorCombo: support d'n'dropping colors to set the color

Copies the approach by KColorButton.

M  +19   -0    src/kcolorcombo.cpp
M  +2    -0    src/kcolorcombo.h

https://invent.kde.org/frameworks/kwidgetsaddons/-/commit/1078245d95a3cb9e9526b218273ad9264157b0f7

diff --git a/src/kcolorcombo.cpp b/src/kcolorcombo.cpp
index af457c60..ec3c6d4e 100644
--- a/src/kcolorcombo.cpp
+++ b/src/kcolorcombo.cpp
@@ -10,11 +10,15 @@
 #include "kcolorcombo.h"
 
 #include "kabstractcoloredit_p.h"
+#include "kcolormimedata_p.h"
 
 #include <QAbstractItemDelegate>
 #include <QApplication>
 #include <QColorDialog>
 #include <QContextMenuEvent>
+#include <QDragEnterEvent>
+#include <QDropEvent>
+#include <QMimeData>
 #include <QStylePainter>
 
 class KColorComboDelegate : public QAbstractItemDelegate
@@ -237,6 +241,8 @@ KColorCombo::KColorCombo(QWidget *parent)
     : QComboBox(parent)
     , d(new KColorComboPrivate(this))
 {
+    setAcceptDrops(true);
+
     setItemDelegate(new KColorComboDelegate(this));
     d->addColors();
 
@@ -329,6 +335,19 @@ void KColorCombo::showEmptyList()
     clear();
 }
 
+void KColorCombo::dragEnterEvent(QDragEnterEvent *event)
+{
+    event->setAccepted(KColorMimeData::canDecode(event->mimeData()) && isEnabled());
+}
+
+void KColorCombo::dropEvent(QDropEvent *event)
+{
+    const QColor c = KColorMimeData::fromMimeData(event->mimeData());
+    if (c.isValid()) {
+        setColor(c);
+    }
+}
+
 void KColorComboPrivate::slotActivated(int index)
 {
     Q_Q(KColorCombo);
diff --git a/src/kcolorcombo.h b/src/kcolorcombo.h
index bf6f7dc6..e759cc6b 100644
--- a/src/kcolorcombo.h
+++ b/src/kcolorcombo.h
@@ -99,6 +99,8 @@ Q_SIGNALS:
     void highlighted(const QColor &col);
 
 protected:
+    void dragEnterEvent(QDragEnterEvent *event) override;
+    void dropEvent(QDropEvent *event) override;
     void paintEvent(QPaintEvent *event) override;
     void contextMenuEvent(QContextMenuEvent *) override;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.