Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt-在QGraphics场景中拖放时如何从项目中获取文本?_C++_Qt_Qt Creator - Fatal编程技术网

C++ Qt-在QGraphics场景中拖放时如何从项目中获取文本?

C++ Qt-在QGraphics场景中拖放时如何从项目中获取文本?,c++,qt,qt-creator,C++,Qt,Qt Creator,我不熟悉Qt Creator和编码,我正在尝试创建一个应用程序,在其中我有一个颜色选项列表和一个区域,我可以拖动这些选项并使用我选择的颜色创建一个图形项目,下面是代码: 我创建了一个QListWidget,现在添加了两个QListWidgetItemiten: OptionList::OptionList(QWidget *parent) : QListWidget(parent) { this->setDragEnabled(true); this->setDropIndicato

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

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

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);
}
然后,我创建了从
qgraphicscene
派生的
MyScene
类,并重新实现了
dragEnterEvent
dragmovevent
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=event->mimeData()->text()但它不工作

我知道它与
qimedata
类有关,但我不知道该怎么办


如何从列表中的项目获取文本并将其传递给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);
    }
}
void MyScene::dragEnterEvent(qgraphicscendragdropevent*事件){
if(event->mimeData()->hasFormat(“应用程序/x-qabstractitemmodeldatalist”))
事件->设置已接受(true);
}
void MyScene::dragMoveEvent(QGraphicsSCeneDragPropertEvent*事件){
if(event->mimeData()->hasFormat(“应用程序/x-qabstractitemmodeldatalist”))
事件->设置已接受(true);
}
void MyScene::dropEvent(QGraphicsSCeneDragPropertEvent*事件){
QByteArray encoded=事件->mimeData()->数据(“应用程序/x-qabstractitemmodeldatalist”);
QDataStream流(编码(&C),QIODevice::ReadOnly);
闪烁的颜色;
而(!stream.atEnd())
{
int row,col;
QMap罗来达美普;
流>>行>>列>>列>>roleDataMap;
颜色场景();
for(常量字符串和颜色:颜色){
块*新块=新块(颜色);
新锁->设置位置(posView);
附加项(新块);
}
}