gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:01:34 26 4
gpt4 key购买 nike

我是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com