Hi all,

I have the following question: I want to drag an item from a list widget to a graphics view and want to show something on the graphics view when the drop ended. For this I have done the regular work, It works, please see the code below. However I have the following question: I would like to show the thingie on the graphics view exactly where the drop ended... and for this I need your help. Seems the dropEvent (and the dragMove event) are not getting the required coordinates ... Can you hint me towards a solution: I want to place the "itm" object exactly where the mouse drag drop ended. (Don't mind the Version class, has nothing to do with this)

Thanks.
f

Code and out below:

Qt Code:
  1. class ERGraphicsScene : public QGraphicsScene
  2. {
  3. public:
  4. ERGraphicsScene(QWidget* parent, Version* v) : QGraphicsScene(parent), itm(0), m_version(v), lastX(-1), lastY(-1)
  5. {
  6. // to mark the centre of the view
  7. addLine(0, -10, 0, 10);
  8. addLine(-10, 0, 10, 0);
  9. }
  10.  
  11. void setLastCoords(int x, int y)
  12. {
  13. if(x != 0) lastX = x;
  14. if(y != 0) lastY = y;
  15.  
  16.  
  17. if(lastX != 0 & &lastY != 0 && lastX != -1 && lastY != -1 && itm != 0)
  18. {
  19. qDebug() << "setLastCoords: X=" << lastX << " Y=" << lastY;
  20.  
  21. itm->setX(lastX);
  22. itm->setY(lastY);
  23. addItem(itm);
  24. itm = 0;
  25. }
  26. }
  27.  
  28. protected:
  29. virtual void dropEvent ( QGraphicsSceneDragDropEvent * event )
  30. {
  31. QString tabName = event->mimeData()->text();
  32. event->acceptProposedAction();
  33.  
  34. itm = m_version->getTable(tabName)->prepareDiagramEntity(event->pos().x(), event->pos().y());
  35. setLastCoords(event->pos().x(), event->pos().y());
  36. }
  37.  
  38. void dragEnterEvent ( QGraphicsSceneDragDropEvent * event )
  39. {
  40. qDebug() << "GraphicsScene::dragEnter : X=" << event->pos().x() << " Y=" << event->pos().y();
  41. event->acceptProposedAction();
  42. }
  43.  
  44. void dragMoveEvent ( QGraphicsSceneDragDropEvent * event )
  45. {
  46. qDebug() << "GraphicsScene::dragMove : X=" << event->pos().x() << " Y=" << event->pos().y();
  47. event->acceptProposedAction();
  48. int x = event->pos().x();
  49. int y = event->pos().y();
  50. setLastCoords(x,y);
  51. }
  52.  
  53. virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
  54. {
  55. qDebug() << "ERGraphicsScene::mouseMove : X=" << event->pos().x() << " Y=" << event->pos().y();
  56. setLastCoords(event->pos().x(), event->pos().y());
  57. }
  58.  
  59. virtual void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
  60. {
  61. qDebug() << "GraphicsScene::mouseRelease: X=" << event->pos().x() << " Y=" << event->pos().y();
  62. }
  63.  
  64. private:
  65. Version* m_version;
  66. int lastX;
  67. int lastY;
  68.  
  69. };
  70.  
  71. class ERGraphicsView : public QGraphicsView
  72. {
  73. public:
  74.  
  75. ERGraphicsView(QWidget* parent, Version* v) : QGraphicsView(parent)
  76. {
  77. setAcceptDrops(true);
  78. scene = new ERGraphicsScene(this, v);
  79. setScene(scene);
  80. }
  81.  
  82.  
  83. void setLastCoords(int x, int y)
  84. {
  85. scene->setLastCoords(x, y);
  86. }
  87.  
  88. protected:
  89.  
  90. virtual void mouseMoveEvent(QMouseEvent *event)
  91. {
  92. qDebug() << "GraphicsView::mouseMove : X=" << event->pos().x() << " Y=" << event->pos().y();
  93. scene->setLastCoords(event->pos().x(), event->pos().y());
  94. }
  95.  
  96. private:
  97.  
  98. ERGraphicsScene* scene;
  99.  
  100. };
  101.  
  102. class TableListWidget : public QListWidget
  103. {
  104. public:
  105.  
  106. TableListWidget(QWidget* parent, ERGraphicsView* associatedGrView) : QListWidget(parent), grv(associatedGrView)
  107. {
  108. setDragEnabled(true);
  109. }
  110.  
  111. protected:
  112. virtual void dragEnterEvent(QDragEnterEvent *e)
  113. {
  114. e->accept();
  115. }
  116.  
  117. virtual void dragMoveEvent(QDragMoveEvent *event)
  118. {
  119. qDebug() << "TabListWidget::dragMove : X=" << event->pos().x() << " Y=" << event->pos().y();
  120. event->accept();
  121. }
  122.  
  123. virtual void dropEvent(QDropEvent *event)
  124. {
  125. event->accept();
  126. }
  127.  
  128. virtual void mouseMoveEvent(QMouseEvent *event)
  129. {
  130. if (!(event->buttons() & Qt::LeftButton)) return;
  131. if (currentItem() == NULL) return;
  132.  
  133. QDrag *drag = new QDrag(this);
  134. QMimeData *mimeData = new QMimeData;
  135.  
  136. // mime stuff
  137. mimeData->setText(currentItem()->text());
  138. drag->setMimeData(mimeData);
  139. drag->setPixmap(IconFactory::getTablesIcon().pixmap(16,16));
  140.  
  141. // start drag
  142. drag->start(Qt::CopyAction | Qt::MoveAction);
  143. }
  144. private:
  145. ERGraphicsView* grv;
  146. };
  147.  
  148. DiagramForm::DiagramForm(Version* v, QWidget *parent) : QWidget(parent), ui(new Ui::DiagramForm), ver(v)
  149. {
  150.  
  151. ui->setupUi(this);
  152.  
  153. ERGraphicsView *graphicsView;
  154. graphicsView = new ERGraphicsView(this, v);
  155. graphicsView->setObjectName(QString::fromUtf8("graphicsView"));
  156. graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
  157. graphicsView->setAcceptDrops(true);
  158.  
  159. TableListWidget *lstTables;
  160. lstTables = new TableListWidget(ui->groupBox, graphicsView);
  161. lstTables->setObjectName(QString::fromUtf8("lstTables"));
  162. QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
  163. sizePolicy.setHorizontalStretch(0);
  164. sizePolicy.setVerticalStretch(0);
  165. sizePolicy.setHeightForWidth(lstTables->sizePolicy().hasHeightForWidth());
  166. lstTables->setSizePolicy(sizePolicy);
  167. lstTables->setDragDropMode(QAbstractItemView::DragDrop);
  168.  
  169. ui->verticalLayout_2->addWidget(lstTables);
  170.  
  171. ui->horizontalLayout->addWidget(graphicsView);
  172.  
  173. for(int i=0; i<v->getTables().size(); i++)
  174. {
  175. QListWidgetItem* qlwi = new QListWidgetItem(v->getTables()[i]->getName(), lstTables);
  176. qlwi->setIcon(IconFactory::getTablesIcon());
  177. }
  178. }
  179.  
  180. DiagramForm::~DiagramForm()
  181. {
  182. delete ui;
  183. }
  184.  
  185. void DiagramForm::changeEvent(QEvent *e)
  186. {
  187. QWidget::changeEvent(e);
  188. switch (e->type()) {
  189. case QEvent::LanguageChange:
  190. ui->retranslateUi(this);
  191. break;
  192. default:
  193. break;
  194. }
  195. }
To copy to clipboard, switch view to plain text mode 

and the output:

TabListWidget::dragMove : X= 197 Y= 46
TabListWidget::dragMove : X= 199 Y= 46
TabListWidget::dragMove : X= 201 Y= 45
TabListWidget::dragMove : X= 203 Y= 45
TabListWidget::dragMove : X= 206 Y= 45
TabListWidget::dragMove : X= 208 Y= 45
TabListWidget::dragMove : X= 210 Y= 45
TabListWidget::dragMove : X= 213 Y= 45
TabListWidget::dragMove : X= 216 Y= 45
TabListWidget::dragMove : X= 218 Y= 45
TabListWidget::dragMove : X= 220 Y= 45
TabListWidget::dragMove : X= 222 Y= 45
TabListWidget::dragMove : X= 224 Y= 45
TabListWidget::dragMove : X= 227 Y= 45
TabListWidget::dragMove : X= 229 Y= 44
TabListWidget::dragMove : X= 231 Y= 44
TabListWidget::dragMove : X= 232 Y= 44
TabListWidget::dragMove : X= 234 Y= 44
TabListWidget::dragMove : X= 236 Y= 44
TabListWidget::dragMove : X= 237 Y= 44
TabListWidget::dragMove : X= 238 Y= 44
TabListWidget::dragMove : X= 239 Y= 44
TabListWidget::dragMove : X= 240 Y= 44
TabListWidget::dragMove : X= 241 Y= 44
TabListWidget::dragMove : X= 243 Y= 44
TabListWidget::dragMove : X= 244 Y= 44
TabListWidget::dragMove : X= 245 Y= 44
TabListWidget::dragMove : X= 246 Y= 44
TabListWidget::dragMove : X= 248 Y= 44
TabListWidget::dragMove : X= 250 Y= 44
TabListWidget::dragMove : X= 252 Y= 44
TabListWidget::dragMove : X= 253 Y= 44
GraphicsScene::dragEnter : X= 0 Y= 0
GraphicsScene::dragMove : X= 0 Y= 0
GraphicsScene::dragMove : X= 0 Y= 0
GraphicsScene::dragMove : X= 0 Y= 0
GraphicsScene::dragMove : X= 0 Y= 0
GraphicsScene::dragMove : X= 0 Y= 0


Here I dropped the object and the dropEvent didn't got any usable coordinates ...