Srv19
Srv19

Reputation: 3618

How to display certain point in QGraphicsScene in the corner of QGraphicsView?

I need QGraphicsView to display part of a scene that is given by its upper-left coordinates, and that scene point must be in the view's corresponding corner.

However i have not found appropriate function to do so out of the box. How can i do that?

Upvotes: 1

Views: 592

Answers (1)

Pavel Strakhov
Pavel Strakhov

Reputation: 40512

By default the view displays scene centered. This can prevent it from fine positioning, so you should change view's alignment:

view->setAlignMent(Qt::AlignLeft | Qt::AlignTop);

When you want some point (in the scene coordinates) to be shown, you need to convert it to viewport coordinates:

QPoint viewport_point = view->mapFromScene(point);

Now you need to set appropriate value of scrollbars:

view->horizontalScrollBar()->setValue(viewport_point.x());
view->verticalScrollBar()->setValue(viewport_point.y());

That should do it. Note that scrolling in the view has limitations. For example, you can't put the scene's right edge to the viewport's left edge. You can make view's sceneRect larger to overcome that.

QGraphicsView::ensureVisible method also may be helpful although it doesn't do exactly what you ask.

Upvotes: 3

Related Questions