c++ - Qt - 在 QGraphicScene 中拖放时如何从项目中获取文本?

标签 c++ qt qt-creator

我是 Qt Creator 和一般编码的新手,我正在尝试创建一个应用程序,其中有一个颜色选项列表和一个区域,我可以在其中拖动这些选项并使用我选择的颜色创建一个图形项目, 这是代码:

我创建了一个 QListWidget 并添加了两个 QListWidgetItem 现在:

OptionList::OptionList(QWidget *parent) : QListWidget(parent)
{
this->setDragEnabled(true);
this->setDropIndicatorShown(true);
this->setSelectionMode(QAbstractItemView::SingleSelection);
this->setDefaultDropAction(Qt::CopyAction);
this->setViewMode(QListView::ListMode);

QListWidgetItem *blue = new QListWidgetItem;
blue->setText("Blue");
blue->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | 
Qt::ItemIsDragEnabled);
addItem(blue);

QListWidgetItem *red = new QListWidgetItem;
red->setText("Red");
red->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | 
Qt::ItemIsDragEnabled);
addItem(red);
}

我创建了一个接收字符串的QgraphicsPathItem,它根据字符串改变颜色

Block::Block(QString color, QGraphicsItem *parent) : QGraphicsPathItem(parent)
{
QPainterPath p;
p.addRoundedRect(0, 0, 150, 50, 2, 2);
setPath(p);
setPen(QPen(Qt::black));
if (color == "Blue")
{
    setBrush(Qt::blue);
}
else if (color == "Red")
{
    setBrush(Qt::red);
}
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);
}

然后,我创建了派生自 QGraphicsSceneMyScene 类,并重新实现了 dragEnterEventdragMoveEventdropEvent

#include "myscene.h"

MyScene::MyScene()
{
setBackgroundBrush(Qt::lightGray);
}

void MyScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
event->setAccepted(true);
}

void MyScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
event->setAccepted(true);
}

void MyScene::dropEvent(QGraphicsSceneDragDropEvent *event)
{
QString color;
color = event->mimeData()->text();

Block *newBlock = new Block(color);

QPointF posView = event->scenePos();
newBlock->setPos(posView);

addItem(newBlock);
}

我尝试使用 QString 颜色; color = event->mimeData()->text(); 但它不起作用

我知道它与 QMimeData 类有关,但我不知道该怎么做

如何从列表中的项目中获取文本并将其传递给 Block 类以更改其颜色?

最佳答案

您必须解码数据,因为 QListWidget 是一个 QAbstractItemView,支持多选。

void MyScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event){
    if(event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist"))
        event->setAccepted(true);
}
void MyScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event){
    if(event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist"))
        event->setAccepted(true);
}
void MyScene::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);
    }
}

关于c++ - Qt - 在 QGraphicScene 中拖放时如何从项目中获取文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50257069/

相关文章:

c++ - 减少参数数量

c++ - 为多个缓冲区分配内存,包括对齐

Qt Key Pressevent Enter

c++ - QtCreator 构建返回 collect2 : ld returned exit status 1

c++ - 调试 QT TimerEvent 时 GDB 崩溃

c++ - 我可以使用 std::shared_ptr 而不是 boost::shared_ptr 构建 boost 库吗

c++ - 通过 C/C++ 连接到 Facebook 的最佳方式是什么?

c++ - QInputDialog 在 getDouble() 中显示逗号而不是点

c++ - QCompleter 用于 QTableWidgetItem 自动完成

c++ - 错误 : collect2: error: ld returned 1 exit status CS106B Stanford Qt Creator