如何同时获得qgraphicsscene和qgraphicsitem滴?

时间:2019-05-06 13:10:25

标签: qt drag-and-drop qgraphicsview qgraphicsscene qgraphicsitem

我在qgraphicsscene上有一些项目,我希望能够将一项拖放到场景的另一部分或空白部分上。根据掉落是发生在另一个项目上还是在场景的空白部分上,发生两种不同的情况。如何处理呢?我已经实现了场景和图形项目的拖放。但是这种方法行不通,因为场景和项目都可以处理掉落事件,对我而言,只能根据掉落位置来处理。 以下是工作示例。 当我将rect1拖放到rect2上时,我应该得到rect2 drop事件。 当我将rect1拖到空白场景上时,应该会出现场景下降事件。

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;
  }

0 个答案:

没有答案