├── MyDropDemo.pro ├── README.md ├── coloritem.cpp ├── coloritem.h ├── dropitem.cpp ├── dropitem.h └── main.cpp /MyDropDemo.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2020-04-06T17:53:33 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = MyDropDemo 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which has been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | CONFIG += c++11 26 | 27 | SOURCES += \ 28 | coloritem.cpp \ 29 | dropitem.cpp \ 30 | main.cpp 31 | 32 | HEADERS += \ 33 | coloritem.h \ 34 | dropitem.h 35 | 36 | # Default rules for deployment. 37 | qnx: target.path = /tmp/$${TARGET}/bin 38 | else: unix:!android: target.path = /opt/$${TARGET}/bin 39 | !isEmpty(target.path): INSTALLS += target 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MyDropDemo 2 | -------------------------------------------------------------------------------- /coloritem.cpp: -------------------------------------------------------------------------------- 1 | #include "coloritem.h" 2 | #include 3 | 4 | ColorItem::ColorItem() 5 | : color(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256)) 6 | { 7 | setToolTip(QString("QColor(%1, %2, %3)\n%4") 8 | .arg(color.red()).arg(color.green()).arg(color.blue()) 9 | .arg("Click and drag this color onto the robot!")); 10 | setCursor(Qt::OpenHandCursor); 11 | setAcceptedMouseButtons(Qt::LeftButton); 12 | } 13 | 14 | QRectF ColorItem::boundingRect() const 15 | { 16 | return QRectF(-15.5, -15.5, 34, 34); 17 | } 18 | 19 | void ColorItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 20 | { 21 | Q_UNUSED(option); 22 | Q_UNUSED(widget); 23 | painter->setPen(Qt::NoPen); 24 | painter->setBrush(Qt::darkGray); 25 | painter->drawEllipse(-12, -12, 30, 30); 26 | painter->setPen(QPen(Qt::black, 1)); 27 | painter->setBrush(QBrush(color)); 28 | painter->drawEllipse(-15, -15, 30, 30); 29 | } 30 | 31 | void ColorItem::mousePressEvent(QGraphicsSceneMouseEvent *) 32 | { 33 | setCursor(Qt::ClosedHandCursor); 34 | } 35 | 36 | void ColorItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) 37 | { 38 | if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton)) 39 | .length() < QApplication::startDragDistance()) { 40 | return; 41 | } 42 | 43 | QDrag *drag = new QDrag(event->widget()); 44 | QMimeData *mime = new QMimeData; 45 | drag->setMimeData(mime); 46 | 47 | static int n = 0; 48 | if (n++ > 2 && QRandomGenerator::global()->bounded(3) == 0) { 49 | QImage image(":/images/head.png"); 50 | mime->setImageData(image); 51 | 52 | drag->setPixmap(QPixmap::fromImage(image).scaled(30, 40)); 53 | drag->setHotSpot(QPoint(15, 30)); 54 | } else { 55 | mime->setColorData(color); 56 | mime->setText(QString("#%1%2%3") 57 | .arg(color.red(), 2, 16, QLatin1Char('0')) 58 | .arg(color.green(), 2, 16, QLatin1Char('0')) 59 | .arg(color.blue(), 2, 16, QLatin1Char('0'))); 60 | 61 | QPixmap pixmap(34, 34); 62 | pixmap.fill(Qt::white); 63 | 64 | QPainter painter(&pixmap); 65 | painter.translate(15, 15); 66 | painter.setRenderHint(QPainter::Antialiasing); 67 | paint(&painter, nullptr, nullptr); 68 | painter.end(); 69 | 70 | pixmap.setMask(pixmap.createHeuristicMask()); 71 | 72 | drag->setPixmap(pixmap); 73 | drag->setHotSpot(QPoint(15, 20)); 74 | } 75 | 76 | drag->exec(); 77 | setCursor(Qt::OpenHandCursor); 78 | } 79 | 80 | void ColorItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *) 81 | { 82 | setCursor(Qt::OpenHandCursor); 83 | } 84 | 85 | 86 | -------------------------------------------------------------------------------- /coloritem.h: -------------------------------------------------------------------------------- 1 | #ifndef COLORITEM_H 2 | #define COLORITEM_H 3 | 4 | #include 5 | 6 | class ColorItem : public QGraphicsItem 7 | { 8 | public: 9 | ColorItem(); 10 | 11 | QRectF boundingRect() const override; 12 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; 13 | 14 | protected: 15 | void mousePressEvent(QGraphicsSceneMouseEvent *event) override; 16 | void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override; 17 | void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; 18 | 19 | private: 20 | QColor color; 21 | }; 22 | 23 | #endif // COLORITEM_H 24 | -------------------------------------------------------------------------------- /dropitem.cpp: -------------------------------------------------------------------------------- 1 | #include "dropitem.h" 2 | #include 3 | 4 | DropItem::DropItem(QGraphicsItem *parent) 5 | : QGraphicsObject(parent), color(Qt::lightGray), dragOver(false) 6 | { 7 | setAcceptDrops(true); 8 | } 9 | 10 | QRectF DropItem::boundingRect() const 11 | { 12 | return QRectF(-100, -100, 200, 200); 13 | } 14 | 15 | void DropItem::paint(QPainter *painter, 16 | const QStyleOptionGraphicsItem *option, QWidget *widget) 17 | { 18 | Q_UNUSED(option); 19 | Q_UNUSED(widget); 20 | painter->setBrush(dragOver ? color.lighter(130) : color); 21 | painter->drawEllipse(-40, -40, 80, 80); 22 | } 23 | 24 | void DropItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) 25 | { 26 | if (event->mimeData()->hasColor()) { 27 | event->setAccepted(true); 28 | dragOver = true; 29 | update(); 30 | } else { 31 | event->setAccepted(false); 32 | } 33 | } 34 | 35 | void DropItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) 36 | { 37 | Q_UNUSED(event); 38 | dragOver = false; 39 | update(); 40 | } 41 | 42 | void DropItem::dropEvent(QGraphicsSceneDragDropEvent *event) 43 | { 44 | dragOver = false; 45 | if (event->mimeData()->hasColor()) 46 | color = qvariant_cast(event->mimeData()->colorData()); 47 | update(); 48 | } 49 | -------------------------------------------------------------------------------- /dropitem.h: -------------------------------------------------------------------------------- 1 | #ifndef MYITEM_H 2 | #define MYITEM_H 3 | 4 | #include 5 | 6 | class DropItem : public QGraphicsObject 7 | { 8 | public: 9 | DropItem(QGraphicsItem *parent = nullptr); 10 | 11 | QRectF boundingRect() const override; 12 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override; 13 | 14 | protected: 15 | void dragEnterEvent(QGraphicsSceneDragDropEvent *event) override; 16 | void dragLeaveEvent(QGraphicsSceneDragDropEvent *event) override; 17 | void dropEvent(QGraphicsSceneDragDropEvent *event) override; 18 | 19 | private: 20 | QColor color; 21 | bool dragOver; 22 | }; 23 | 24 | #endif // MYITEM_H 25 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "coloritem.h" 4 | #include "dropitem.h" 5 | 6 | class GraphicsView : public QGraphicsView 7 | { 8 | public: 9 | GraphicsView(QGraphicsScene *scene) : QGraphicsView(scene) 10 | { 11 | } 12 | 13 | protected: 14 | void resizeEvent(QResizeEvent *) override 15 | { 16 | } 17 | }; 18 | 19 | int main(int argc, char *argv[]) 20 | { 21 | QApplication a(argc, argv); 22 | 23 | QGraphicsScene scene(-200, -200, 400, 400); 24 | 25 | for (int i = 0; i < 10; ++i) { 26 | ColorItem *colorItem = new ColorItem; 27 | colorItem->setPos(::sin((i * 6.28) / 10.0) * 150, 28 | ::cos((i * 6.28) / 10.0) * 150); 29 | 30 | scene.addItem(colorItem); 31 | } 32 | 33 | DropItem *dropItem = new DropItem; 34 | dropItem->setPos(0, 0); 35 | scene.addItem(dropItem); 36 | 37 | GraphicsView view(&scene); 38 | view.setRenderHint(QPainter::Antialiasing); 39 | view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); 40 | view.setBackgroundBrush(QColor(230, 200, 167)); 41 | view.setWindowTitle("My Drop Test"); 42 | view.show(); 43 | 44 | return a.exec(); 45 | } 46 | 47 | 48 | 49 | 50 | --------------------------------------------------------------------------------