当前位置:   article > 正文

Qt小例子学习61 - 拖动显示显示图像/图标/数据_qt drop 显示图标

qt drop 显示图标

Qt小例子学习61 - 拖动显示显示图像/图标/数据

block.h

#ifndef BLOCK_H
#define BLOCK_H

#include <QGraphicsPathItem>

class Block : public QGraphicsPathItem
{
public:
    Block(QString color, QGraphicsItem *parent = nullptr);
};

#endif // BLOCK_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

block.cpp

#include "block.h"

#include <QPen>

Block::Block(QString color, QGraphicsItem *parent) : QGraphicsPathItem(parent)
{
    QPainterPath p;
    p.addRoundedRect(0, 0, 150, 50, 2, 2);
    setPath(p);
    setPen(QPen(Qt::black));
    setBrush(QColor(color.toLower()));
    setFlag(QGraphicsItem::ItemIsMovable);
    setFlag(QGraphicsItem::ItemIsSelectable);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

OptionList.h

#ifndef OPTIONLIST_H
#define OPTIONLIST_H
#include <QListWidget>
class OptionList : public QListWidget
{
public:
    OptionList(QWidget *parent = nullptr);
protected:
    void startDrag(Qt::DropActions supportedActions);
};
#endif // OPTIONLIST_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

optionlist.cpp

#include "optionlist.h"

#include <QDrag>

OptionList::OptionList(QWidget *parent) : QListWidget(parent)
{

    setDragEnabled(true);
    setDropIndicatorShown(true);
    setSelectionMode(QAbstractItemView::SingleSelection);
    setDefaultDropAction(Qt::CopyAction);
    setViewMode(QListView::ListMode);

    for (const QString &color : {"Blue", "Red", "Green", "Yellow"})
    {
        QListWidgetItem *blue = new QListWidgetItem;
        blue->setText(color);
        blue->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable |
                       Qt::ItemIsDragEnabled);
        addItem(blue);
    }
}

void OptionList::startDrag(Qt::DropActions supportedActions)
{
    if (supportedActions & Qt::CopyAction)
    {
        QList<QListWidgetItem *> m_items = selectedItems();
        if (m_items.isEmpty())
            return;
        QMimeData *data = mimeData(m_items);
        QDrag *drag = new QDrag(this);
        QPixmap pixmap(":/images/MyIcon_icon.png");
        drag->setPixmap(pixmap);
        drag->setMimeData(data);
        drag->setHotSpot(pixmap.rect().center());
        drag->exec(Qt::CopyAction);
    }
    else
        QListWidget::startDrag(supportedActions);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

Scene.h

#ifndef SCENE_H
#define SCENE_H

#include <QGraphicsScene>
class QGraphicsSceneDragDropEvent;

class Scene : public QGraphicsScene
{
public:
    Scene(QObject *parent = nullptr);

protected:
    void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
    void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
    void dropEvent(QGraphicsSceneDragDropEvent *event);
};

#endif // SCENE_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

scene.cpp

#include "scene.h"
#include "block.h"

#include <QGraphicsSceneDragDropEvent>
#include <QMimeData>

Scene::Scene(QObject *parent) : QGraphicsScene(parent)
{
    setBackgroundBrush(Qt::lightGray);
}

void Scene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
    if (event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist"))
        event->setAccepted(true);
}

void Scene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
    if (event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist"))
        event->setAccepted(true);
}

void Scene::dropEvent(QGraphicsSceneDragDropEvent *event)
{

    QByteArray encoded =
        event->mimeData()->data("application/x-qabstractitemmodeldatalist");
    QDataStream stream(&encoded, QIODevice::ReadOnly);

    QStringList colors;

    while (!stream.atEnd())
    {
        int row, col;
        QMap<int, QVariant> roleDataMap;
        stream >> row >> col >> roleDataMap;
        colors << roleDataMap[Qt::DisplayRole].toString();
    }
    QPointF posView = event->scenePos();
    for (const QString &color : colors)
    {
        Block *newBlock = new Block(color);
        newBlock->setPos(posView);
        addItem(newBlock);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

main.cpp

#include "optionlist.h"
#include "scene.h"

#include <QApplication>
#include <QGraphicsView>
#include <QHBoxLayout>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w;

    QHBoxLayout lay(&w);

    OptionList list;

    QGraphicsView view;
    view.setScene(new Scene);
    lay.addWidget(&list);
    lay.addWidget(&view);
    w.show();

    return a.exec();
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/764653
推荐阅读
  

闽ICP备14008679号