I developing some kind of builder for our project. I want to use both drag and drop support and context menu in my application. Currently I use drag and drop support but no luck with context menu. Below is my builders gui.



Left side my gui is toolbox. I am draging and droping widgets to the right side(QGraphicsScene) and I also want to use context menu inside QGraphicsScene. I use context menu inside graphics scene before. But before I did not use drap & drop operations. I write proper code but it does not work. What is missing?(Is it related drag & drop). Below is my code files.

Qt Code:
  1. //#dragwidget.cpp - Toolbox widgets
  2. #include "dragwidget.h"
  3.  
  4. DragWidget::DragWidget(void)
  5. {
  6. }
  7.  
  8. DragWidget::~DragWidget(void)
  9. {
  10. }
  11.  
  12. void DragWidget::mousePressEvent(QMouseEvent *ev)
  13. {
  14. if(ev->button() == Qt::LeftButton)
  15. {
  16. QMimeData *data = new QMimeData;
  17. data->setProperty("type",property("type"));
  18. QDrag *drag = new QDrag(this);
  19. drag->setMimeData(data);
  20. drag->start();
  21. }
  22. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. //#view.cpp - Wrapper class for QGraphicsView
  2.  
  3. #include "view.h"
  4.  
  5. View::View(Scene *scene,QWidget *parent)
  6. :QGraphicsView(scene,parent)
  7. {
  8. }
  9.  
  10. View::~View()
  11. {
  12. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. //#scene.cpp - Wrapper class for QGraphicsScene which catch drop events
  2.  
  3. #include "scene.h"
  4.  
  5. Scene::Scene(QObject *parent)
  6. {
  7. }
  8.  
  9. Scene::~Scene(void)
  10. {
  11. }
  12.  
  13. void Scene::setSceneRect(qreal x, qreal y, qreal w, qreal h)
  14. {
  15. QGraphicsScene::setSceneRect(x,y,w,h);
  16. }
  17.  
  18. void Scene::dragEnterEvent(QGraphicsSceneDragDropEvent *e)
  19. {
  20. if(e->mimeData()->hasFormat("text/plain"));
  21. e->acceptProposedAction();
  22. }
  23.  
  24. void Scene::dragMoveEvent(QGraphicsSceneDragDropEvent *e)
  25. {
  26. }
  27.  
  28. void Scene::dropEvent(QGraphicsSceneDragDropEvent *e)
  29. {
  30. e->acceptProposedAction();
  31. int itemType = e->mimeData()->property("type").toInt();
  32. switch(itemType)
  33. {
  34. case BlockSegment:
  35. drawStandartBlockSegment(e->scenePos());
  36. break;
  37. case Switch:
  38. drawStandartSwitch(e->scenePos());
  39. break;
  40. case Signal:
  41. drawStandartSignal(e->scenePos());
  42. break;
  43. }
  44. }
  45.  
  46. void Scene::drawStandartBlockSegment(QPointF p)
  47. {
  48. BlockSegmentItem *blockSegment = new BlockSegmentItem(p.x(),p.y(),p.x()+100,p.y());
  49. addItem(blockSegment);
  50. }
  51.  
  52. void Scene::drawStandartSwitch(QPointF p)
  53. {
  54. SwitchItem *switchItem = new SwitchItem(":app/SS.svg");
  55. switchItem->setPos(p);
  56. addItem(switchItem);
  57. }
  58.  
  59. void Scene::drawStandartSignal(QPointF p)
  60. {
  61. SignalItem *signalItem = new SignalItem(":app/sgs3lr.svg");
  62. signalItem->setPos(p);
  63. addItem(signalItem);
  64. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. //#item.cpp - Base class for my custom graphics scene items
  2.  
  3. #include "item.h"
  4.  
  5. Item::Item(ItemType itemType, QGraphicsItem *parent)
  6. :QGraphicsWidget(parent),m_type(itemType)
  7. {
  8. }
  9.  
  10. Item::~Item()
  11. {
  12. }
  13.  
  14. int Item::type()
  15. {
  16. return m_type;
  17. }
  18.  
  19. QRectF Item::boundingRect() const
  20. {
  21. return QRectF(0,0,0,0);
  22. }
  23.  
  24. QPainterPath Item::shape() const
  25. {
  26. return path;
  27. }
  28.  
  29. void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  30. {
  31. Q_UNUSED(painter);
  32. Q_UNUSED(option);
  33. Q_UNUSED(widget);
  34. }
  35.  
  36. void Item::deleteItem()
  37. {
  38.  
  39. }
  40.  
  41. void Item::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
  42. {
  43. Q_UNUSED(event);
  44. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. //#switchitem.cpp - my custom switch item.
  2.  
  3. #include "switchitem.h"
  4.  
  5. SwitchItem::SwitchItem(const QString& fileName,QGraphicsItem *parent /* = 0 */)
  6. :Item(Switch,parent)
  7. {
  8. svg = new QGraphicsSvgItem(fileName,this);
  9. createActions();
  10. createContextMenu();
  11. }
  12.  
  13. SwitchItem::~SwitchItem(void)
  14. {
  15. }
  16.  
  17. QRectF SwitchItem::boundingRect() const
  18. {
  19. return QRectF(pos().x(),pos().y(),svg->boundingRect().width(),
  20. svg->boundingRect().height());
  21. }
  22.  
  23. QPainterPath SwitchItem::shape() const
  24. {
  25. path.addRect(boundingRect());
  26. return path;
  27. }
  28.  
  29. void SwitchItem::createActions()
  30. {
  31. deleteAct = new QAction("Sil",this);
  32. deleteAct->setIcon(QIcon(":app/delete.png"));
  33. connect(deleteAct,SIGNAL(triggered()),this,SLOT(deleteItem()));
  34. }
  35.  
  36. void SwitchItem::createContextMenu()
  37. {
  38. contextMenu = new QMenu("SwitchContextMenu");
  39. contextMenu->addAction(deleteAct);
  40. }
  41.  
  42. void SwitchItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *e)
  43. {
  44. contextMenu->exec(e->screenPos());
  45. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. //#app.cpp - Application class
  2.  
  3. #include "app.h"
  4. #include "dragwidget.h"
  5.  
  6. App::App(QWidget *parent, Qt::WFlags flags)
  7. : QMainWindow(parent, flags)
  8. {
  9. Q_INIT_RESOURCE(app);
  10. ui.setupUi(this);
  11. canvasScene = new Scene(this);
  12. canvasScene->setSceneRect(0,0,5000,5000);
  13. canvasView = new View(canvasScene);
  14. canvasView->setBackgroundBrush(Qt::black);
  15. canvasView->ensureVisible(0,0,canvasView->geometry().width(),canvasView->geometry().height());
  16. QHBoxLayout *layout = new QHBoxLayout;
  17. toolBox = new QFrame(this);
  18. layout->addWidget(toolBox,1);
  19. layout->addWidget(canvasView,7);
  20. ui.centralWidget->setLayout(layout);
  21. createToolBox();
  22.  
  23. }
To copy to clipboard, switch view to plain text mode