I am creating a custom color picker by sub classing QFrame. There are three main functionality I would be needing.

1. A color picker
2. dragdrop color to other picker
3. right click menu

There won't be any problem in 1 and 3. How ever for 2 there are:

When you drop color to target, a menu appears with two options: swap & replace
If user select "swap", it should swap the colors of source and target
If user select "replace", it should replace the color of target with source.

drag & drop is having some problem:
Qt Code:
  1. void DragWidget::dragEnterEvent(QDragEnterEvent *event)
  2. {
  3. if (event->mimeData()->hasColor()) {
  4. if (event->source() == this) {
  5. event->setDropAction(Qt::MoveAction);
  6. event->accept();
  7. }
  8. else {
  9. event->acceptProposedAction();
  10. }
  11. }
  12. else {
  13. event->ignore();
  14. }
  15. }
  16.  
  17. void DragWidget::dragMoveEvent(QDragMoveEvent *event)
  18. {
  19. if (event->mimeData()->hasColor()) {
  20. if (event->source() == this) {
  21. event->setDropAction(Qt::MoveAction);
  22. event->accept();
  23. }
  24. else {
  25. event->acceptProposedAction();
  26. }
  27. }
  28. else {
  29. event->ignore();
  30. }
  31.  
  32. }
  33.  
  34. void DragWidget::dropEvent(QDropEvent *event)
  35. {
  36. if (event->mimeData()->hasColor()) {
  37. myColor = qvariant_cast<QColor>(event->mimeData()->colorData());
  38.  
  39. if (event->source() == this) {
  40. event->setDropAction(Qt::MoveAction);
  41. event->accept();
  42. } else {
  43. event->acceptProposedAction();
  44. }
  45. } else {
  46. event->ignore();
  47. }
  48. }
  49.  
  50.  
  51. void DragWidget::mousePressEvent(QMouseEvent *event)
  52. {
  53. QMimeData *mimeData = new QMimeData;
  54. mimeData->setColorData(QVariant(myColor));
  55.  
  56. QDrag *drag = new QDrag(this);
  57. drag->setMimeData(mimeData);
  58. }
To copy to clipboard, switch view to plain text mode