If I understand correctly, you want to draw a horizontal line and a vertical line intersecting at the cursor position and the size of the viewport.
A clear solution would be to override QGraphicsScene :: drawForeground () to draw two lines using the painter.
The problem is that the scene is not aware of the position of the mouse. This means that the view will have to track it and report the scene when the mouse position has changed.
To do this, you need to create your own GraphicsScene (inheriting from QGraphicsScene ) and your own GraphicsView (inheriting from QGraphicsView ).
On your GraphicsView constructor, you will need to start mouse tracking. This will force you to receive mouseMoveEvent each time the mouse moves inside the view:
GraphicsViewTrack::GraphicsViewTrack(QWidget* parent) : QGraphicsView(parent) { setMouseTracking(true); } void GraphicsViewTrack::mouseMoveEvent(QMouseEvent* pEvent) { QPointF MousePos = this->mapToScene(pEvent->pos()); emit mousePosChanged(MousePos.toPoint()); }
As you can see in the above code snippet, the view emits a signal ( mousePosChanged ) to which the scene will connect. This signal contains the mouse position converted to the scene coordinates.
Now, on the scene side, you need to add a slot that will be called when the mouse position changes, save the new mouse position in the member variable and override QGraphicsScene :: drawForeground () :
void GraphicsSceneCross::drawForeground(QPainter* painter, const QRectF& rect) { QRectF SceneRect = this->sceneRect(); painter->setPen(QPen(Qt::black, 1)); painter->drawLine(SceneRect.left(), m_MousePos.y(), SceneRect.right(), m_MousePos.y()); painter->drawLine(m_MousePos.x(), SceneRect.top(), m_MousePos.x(), SceneRect.bottom()); } void GraphicsSceneCross::onMouseChanged(QPoint NewMousePos) { m_MousePos = NewMousePos;
The last thing to do is connect the GraphicsView signal to the GraphicsScene slot.
I will let you check if this solution is acceptable.