我已将graphicsview(附加的graphicscene)设置为场景中的中央部件,我添加了2个rect项目(graphicsItem)(均可拖动)。当我拖动1个矩形项目时,它的场景会发生拖动输入事件,但是当我拖动到其他矩形项目时,它不会发生拖动输入事件。如何解决呢? 下面是工作示例。
graphicsview.h
class GraphicsView : public QGraphicsView
{
public:
GraphicsView();
};
graphicsview.cpp
GraphicsView::GraphicsView()
{
//setAcceptDrops(true);
}
graphicsScene.h
class GraphicsScene : public QGraphicsScene
{
public:
GraphicsScene();
protected:
void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
void dropEvent(QGraphicsSceneDragDropEvent *event);
};
graphicsScene.cpp
GraphicsScene::GraphicsScene()
{
}
void GraphicsScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
qDebug() << "Scene::dragEnterEvent";
event->acceptProposedAction();
}
void GraphicsScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
qDebug() << "Scene::dragMoveEvent";
event->acceptProposedAction();
}
void GraphicsScene::dropEvent(QGraphicsSceneDragDropEvent *event)
{
qDebug() << "Scene::dropEvent";
event->acceptProposedAction();
}
graphicsRectItem.h
class GraphicsRectItem : public QGraphicsRectItem
{
public:
GraphicsRectItem();
static QString MimeType() { return QStringLiteral("job/x-job"); }
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
void dropEvent(QGraphicsSceneDragDropEvent *event);
};
graphicsRectItem.cpp
GraphicsRectItem::GraphicsRectItem()
{
setAcceptDrops(true);
}
void GraphicsRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "RectItem::mousePressEvent";
setCursor(Qt::ClosedHandCursor);
}
void GraphicsRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "RectItem::mouseMoveEvent";
if (QLineF(event->screenPos(), event-
>buttonDownScreenPos(Qt::LeftButton))
.length() < QApplication::startDragDistance()) {
QGraphicsRectItem::mouseMoveEvent(event);
return;
}
qDebug() << "drag before executed";
QByteArray itemData;
QString m_jobName = "rect";
DataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << m_jobName ;
QMimeData *mimeData = new QMimeData;
mimeData->setData(GraphicsRectItem::MimeType(), itemData);
QDrag *drag = new QDrag(event->widget());
drag->setMimeData(mimeData);
drag->exec();
qDebug() << "drag executed";
setCursor(Qt::OpenHandCursor);
}
void GraphicsRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "RectItem::mouseReleaseEvent";
setCursor(Qt::OpenHandCursor);
}
void GraphicsRectItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
qDebug() << "RectItem::dragEnterEvent";
event->acceptProposedAction();
}
void GraphicsRectItem::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
qDebug() << "RectItem::dragMoveEvent";
event->acceptProposedAction();
}
void GraphicsRectItem::dropEvent(QGraphicsSceneDragDropEvent *event)
{
qDebug() << "RectItem::dropEvent";
event->acceptProposedAction();
}
Mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_pView = new GraphicsView;
m_pScene = new GraphicsScene;
m_pView->setScene(m_pScene);
GraphicsRectItem *rect1 = new GraphicsRectItem;
rect1->setRect(10,10,55,55);
GraphicsRectItem *rect2 = new GraphicsRectItem;
rect2->setRect(75,10,55,55);
m_pScene->addItem(rect1);
m_pScene->addItem(rect2);
this->setCentralWidget(m_pView);
}
MainWindow::~MainWindow()
{
delete ui;
}