【发布时间】:2019-11-15 20:27:58
【问题描述】:
尝试将标签从 QWidget 拖放到 QGraphicsScene。我已经从 QLabel 进行了自定义实现。 QGraphicsView 也接受丢弃。问题是 QGraphicsScene 在将元素拖放到它时不会收到任何事件。
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();
}
我需要做什么才能让场景接收掉落?
【问题讨论】:
-
QGraphicsView 是否可能“接受”下降,因此不会将其传播到场景?