Qt - 在QGraphicScene中拖放时如何从项目中获取文本?
Posted
技术标签:
【中文标题】Qt - 在QGraphicScene中拖放时如何从项目中获取文本?【英文标题】:Qt - How to get the text from an item when dragging and dropping in a QGraphicScene? 【发布时间】:2018-05-09 15:31:23 【问题描述】:我是 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);
然后,我创建了从QGraphicsScene
派生的MyScene
类,并重新实现了dragEnterEvent
、dragMoveEvent
和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);
我尝试使用QString color; color = event->mimeData()->text();
,但它不起作用
我知道这与 QMimeData
类有关,但我不知道该怎么办
如何从列表中的项目中获取文本并将其传递给 Block 类以更改其颜色?
【问题讨论】:
【参考方案1】:您必须解码数据,因为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);
【讨论】:
以上是关于Qt - 在QGraphicScene中拖放时如何从项目中获取文本?的主要内容,如果未能解决你的问题,请参考以下文章