2

If I set a pixmap to a QGraphicsScene that is larger that the window, it will add scrollbars, So is it possible to get what is displayed in the QGraphicsView ?

Also Is there any event raised while scrolling and changeing the displayed area ?

Here is a screen of what I'm talking about: enter image description here

Thanks

rednaks
  • 1,982
  • 1
  • 17
  • 23

2 Answers2

10

You can determine visible area in the scene coordinates as follows:

QRect viewport_rect(0, 0, view->viewport()->width(), view->viewport()->height());
QRectF visible_scene_rect = view->mapToScene(viewport_rect).boundingRect();

You can use QGraphicsItem::mapFromScene to convert scene coordinates to item coordinates is necessary.

You can use view->horizontalScrollBar() and view->verticalScrollBar() to obtain QScrollBar* objects. Connect to valueChanged(int) signal of these objects to track scrolling.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • Are you sure about the QScrollBar connections ? I got this while compiling : `connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), ((QGraphicsView*)this), SLOT(scrollMoved(int)));` `graphicScene.cpp:30:107: error: no matching function for call to ‘GraphicScene::connect(QScrollBar*, const char [19], QGraphicsView*, const char [18])’` – rednaks Jul 29 '13 at 19:15
  • Sorry, I forgot to include `QScrollBar` :) – rednaks Jul 29 '13 at 20:04
  • Man thanks a lot, you saved me some hours :D i tried tons of different solutions. – retinotop Feb 06 '19 at 14:28
1

Also, note the viewport contains a function to fit an area of the scene into the view so that you can see all of that area using: -

void QGraphicsView::fitInView(const QRectF & rect, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • Incorrect. `sceneRect` returns the total area that can be visible in the view on all scrollbar positions. The scene rect can be larger than viewport and doesn't depend on scrollbar positions. – Pavel Strakhov Jul 29 '13 at 16:09
  • Oops, my mistake. You're right, thanks for pointing it out. I've edited that part of the answer. – TheDarkKnight Jul 29 '13 at 16:11