HI,

I am trying to capture the mouse position relative to a graphicsscene. See code below, the X and Y values in the bottom right are for the view, I would like the values for the scene, I so when I hover ove the top right cross is says 250,250 etc.:

I am stumped any help would be appreciated.

Thanks,

Pete.

Qt Code:
  1. #include <QApplication>
  2. #include <QMainWindow>
  3. #include <QStatusBar>
  4. #include <QGraphicsView>
  5. #include <QGraphicsScene>
  6. #include <QGraphicsSceneMouseEvent>
  7. #include <QGraphicsTextItem>
  8. #include <QMouseEvent>
  9. #include <QScrollBar>
  10. #include <QLabel>
  11. #include <math.h>
  12. #include <QtGui>
  13.  
  14.  
  15. class MapView : public QGraphicsView
  16. {
  17.  
  18. public:
  19. MapView(QWidget *parent = 0);
  20. QPoint sceneCoordinates;
  21. QLabel *coordinateDisplayLabel;
  22.  
  23. protected:
  24. void scaleView(qreal scaleFactor);
  25. void wheelEvent(QWheelEvent *event);
  26. void mousePressEvent(QMouseEvent *event);
  27. void mouseMoveEvent(QMouseEvent *event);
  28. void mouseReleaseEvent(QMouseEvent *event);
  29. void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
  30. void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
  31.  
  32.  
  33. private:
  34. QString cursorXString;
  35. QString cursorYString;
  36. void storeMouseEvent(QMouseEvent *event);
  37. bool scrolling;
  38. QMouseEvent lastMouseEvent;
  39.  
  40. };
  41.  
  42. MapView::MapView(QWidget *parent)
  43. : QGraphicsView(parent), lastMouseEvent(QEvent::None,QPoint(),
  44. Qt::NoButton,Qt::NoButton,Qt::NoModifier)
  45.  
  46. {
  47. setAttribute (Qt::WA_SetCursor);
  48. setCursor(Qt::CrossCursor);
  49. scale(1,-1);
  50. coordinateDisplayLabel = new QLabel(0);
  51. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  52. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
  53. setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
  54. scrolling = false;
  55. }
  56.  
  57. void MapView::wheelEvent(QWheelEvent *event)
  58. {
  59. scaleView(pow((double)2, event->delta() / 240.0));
  60. }
  61.  
  62. void MapView::scaleView(qreal scaleFactor)
  63. {
  64. qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
  65. if (factor < 0.07 || factor > 100)
  66. return;
  67. scale(scaleFactor, scaleFactor);
  68. }
  69.  
  70. void MapView::storeMouseEvent(QMouseEvent *event)
  71. {
  72. lastMouseEvent = QMouseEvent(event->type(), event->pos(), event->globalPos(),
  73. event->button(),event->buttons(),event->modifiers());
  74.  
  75. }
  76.  
  77. void MapView::mousePressEvent(QMouseEvent *event)
  78. {
  79. if(event->button() == Qt::MidButton)
  80. {
  81. setCursor(Qt::ClosedHandCursor);
  82. scrolling = true;
  83. storeMouseEvent(event);
  84. event->accept();
  85. return;
  86. }
  87. }
  88. void MapView::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
  89. {
  90. //QGraphicsScene::mousePressEvent(mouseEvent);
  91. qDebug() << mouseEvent->scenePos();
  92.  
  93. }
  94. void MapView::mouseMoveEvent(QMouseEvent *event)
  95. {
  96.  
  97. if(scrolling)
  98. {
  99. QPoint delta = event->globalPos() - lastMouseEvent.globalPos();
  100. QScrollBar *hBar = horizontalScrollBar();
  101. QScrollBar *vBar = verticalScrollBar();
  102. hBar->setValue(hBar->value() - delta.x());
  103. vBar->setValue(vBar->value() - delta.y());
  104. storeMouseEvent(event);
  105. event->accept();
  106. return;
  107. }
  108.  
  109. sceneCoordinates = mapFromScene(event->pos());
  110. cursorXString.setNum(sceneCoordinates.x());
  111. cursorYString.setNum(sceneCoordinates.y());
  112. coordinateDisplayLabel->setText("x = " + cursorXString + " " + "y = " + cursorYString);
  113. }
  114.  
  115. void MapView::mouseReleaseEvent(QMouseEvent *event)
  116. {
  117. if((event->button() == Qt::MidButton) && (scrolling == true))
  118. {
  119. setCursor(Qt::CrossCursor);
  120. scrolling = false;
  121. storeMouseEvent(event);
  122. event->accept();
  123. return;
  124. }
  125. event->ignore();
  126. }
  127.  
  128.  
  129. int main( int argc, char **argv )
  130. {
  131. QApplication a( argc, argv );
  132. MapView *view = new MapView;
  133. QStatusBar *statusbar = new QStatusBar;
  134.  
  135. scene->setSceneRect(-30000, -30000, 60000, 60000);
  136. scene->setBackgroundBrush(Qt::black);
  137.  
  138. scene->addLine ( -50, 0, 50, 0, (QPen(Qt::white, 0)));
  139. scene->addLine ( 0, -50, 0, 50, (QPen(Qt::white, 0)));
  140. QGraphicsTextItem item1("0,0");
  141. item1.setDefaultTextColor(Qt::white);
  142. item1.setPos(0,0);
  143. item1.scale(1,-1);
  144. scene->addItem(&item1);
  145.  
  146. scene->addLine ( 200, 250, 300, 250, (QPen(Qt::white, 0)));
  147. scene->addLine ( 250, 300, 250, 200, (QPen(Qt::white, 0)));
  148. QGraphicsTextItem item2("250,250");
  149. item2.setDefaultTextColor(Qt::white);
  150. item2.setPos(250,250);
  151. item2.scale(1,-1);
  152. scene->addItem(&item2);
  153.  
  154. view->setScene(scene);
  155. mw->setCentralWidget(view);
  156. statusbar->addPermanentWidget(view->coordinateDisplayLabel, 0);
  157. mw->setStatusBar(statusbar);
  158.  
  159. mw->show();
  160.  
  161. return a.exec();
  162. }
To copy to clipboard, switch view to plain text mode