C++ 接受拖动&;转到qgraphicscene

C++ 接受拖动&;转到qgraphicscene,c++,qt,C++,Qt,正在尝试将标签从QWidget拖放到QGraphicscene。我已经从QLabel定制了实现。此外,QGraphicsView正在接受删除。问题是QGraphicscene在将元素拖放到其中时不会获得任何事件 QLabel实现.cpp void ItemLabel::mousePressEvent(QMouseEvent *ev) { if(ev->button() == Qt::LeftButton){ QDrag *drag = new QDrag(this);

正在尝试将标签从QWidget拖放到QGraphicscene。我已经从QLabel定制了实现。此外,QGraphicsView正在接受删除。问题是QGraphicscene在将元素拖放到其中时不会获得任何事件

QLabel实现.cpp

void ItemLabel::mousePressEvent(QMouseEvent *ev)
{
    if(ev->button() == Qt::LeftButton){
       QDrag *drag = new QDrag(this);
       QMimeData *mimeData = new QMimeData;

       mimeData->setText("Test");
       drag->setMimeData(mimeData);
       drag->setPixmap(*this->pixmap());
       drag->exec();

       Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction);
    }
}
场景.cpp

void Scene::dropEvent(QGraphicsSceneDragDropEvent  *event)
{
    event->acceptProposedAction();
    qDebug() << "drop";
}

void Scene::dragEnterEvent(QGraphicsSceneDragDropEvent  *event)
{
    if(event->mimeData()->hasFormat("text/plain"))
        event->acceptProposedAction();
}
void场景::dropEvent(QGraphicsSceneDragDropEvent*事件)
{
事件->AcceptProposeAction();
qDebug()mimeData()->hasFormat(“文本/普通”))
事件->AcceptProposeAction();
}

获取场景接收拖放需要做什么?

您应该扩展
QGraphicsView
,设置
qgraphicscene
,然后覆盖
QGraphicsView
拖放槽

看看这个工作示例:

MyGraphicsView::MyGraphicsView(QObject *p_parent):
    QGraphicsView(),
    m_scene(nullptr)
{ 

    ...
    m_scene = new QGraphicsScene(rect(), p_parent);
    setScene(m_scene);
    setAcceptDrops(true);
}


void MyGraphicsView::dragEnterEvent(QDragEnterEvent *p_event)
{
    p_event->acceptProposedAction();
}

void MyGraphicsView::dragMoveEvent(QDragMoveEvent *p_event)
{
    p_event->acceptProposedAction();
}

void MyGraphicsView::dropEvent(QDropEvent *p_event)
{
    p_event->acceptProposedAction();
}
如果在
dragEnter
dragMove
中不
AcceptProposeAction()
,将永远不会触发
dropEvent

QGraphicsView是否可能“接受”拖放,从而不会将其传播到场景?