Gabriel_Br
Gabriel_Br

Reputation: 119

Qt - How to get the text from an item when dragging and dropping in a QGraphicScene?

I'am new to Qt Creator and coding in general and I'am trying to create an application where I have a list of color options and an area where I can drag these options and create a graphic item with the color I chose, here's the code:

I created a QListWidget and added two QListWidgetItem itens for now:

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);
}

I Created a QgraphicsPathItem that receives a string, depending on the string, it changes color

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);
}

then, I created MyScene class derived from QGraphicsScene, and reimplemented the dragEnterEvent, dragMoveEvent and dropEvent

#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);
}

I tried using QString color; color = event->mimeData()->text(); but it isn't working

I know it has something to do with the QMimeData class but I don't know what to do

How can I get the text from the item in the list and pass it to the Block class to change its color?

Upvotes: 1

Views: 765

Answers (1)

eyllanesc
eyllanesc

Reputation: 243917

You have to decode the data since QListWidget, which is a QAbstractItemView, supports multi-selection.

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);
    }
}

Upvotes: 1

Related Questions