1

I want to track mouse in my main window. I have enabled the moustracking in the QGraphicsView here is the constructor for GraphicsView subclass, the rest is the default behavior.

GraphicsView::GraphicsView(QWidget* parent): QGraphicsView(parent)
{

  setMouseTracking(true);

  setDragMode(RubberBandDrag);
  setRenderHints(QPainter::Antialiasing|  QPainter::TextAntialiasing);
  setMinimumSize(600, 400);

}

here is my GraphicsScene MouseMove method:

void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
  if (myMode == InsertLine && line != nullptr) {
      QLineF newLine(line->line().p1(), mouseEvent->scenePos());
      line->setLine(newLine);
  } else if (myMode == Select) {
      QGraphicsScene::mouseMoveEvent(mouseEvent);
  }
  QPointF point = mouseEvent->pos();
  //point = this->mapToScene(point);
  qDebug() << point.x() << " " << point.y() << " ";
  mouseMoved(point);
  QGraphicsScene::mouseMoveEvent(mouseEvent);
}

I get zero and zero for x and y position. What am i doing wrong ?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Ring Zero.
  • 459
  • 3
  • 12

1 Answers1

1

If you review the docs of pos() method of QGraphicsSceneMouseEvent:

Returns the mouse cursor position in item coordinates.

That is, those coordinates are relative to a QGraphicsItem, but in this case there are no coordinates and consequently it has no meaning (it would only have it if the mouseMoveEvent would belong to a QGraphicsItem). In this case you must use the scenePos() method

void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
  // ...
  QPointF point = mouseEvent->scenePos();
  qDebug() << point.x() << " " << point.y() << " ";
  // ...
  QGraphicsScene::mouseMoveEvent(mouseEvent);
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241