18

I've been pulling my hair out with this one for hours. There's a thread here about it, but nothing seems to be working. QGraphicsView::rect() will return the width and height, but the left and top values aren't set properly (always 0 -- ignoring the scrolled amount). I want it in scene coordinates, but it should be easy enough to translate from any system. I have no idea what horizontalScrollBar()->value() and vert are returning...seems to be meaningless jibberish.


@fabrizioM:

// created here
void EditorWindow::createScene() {
    m_scene = new EditorScene(this);
    m_view = new EditorView(m_scene);
    setCentralWidget(m_view);
    connect(m_scene, SIGNAL(mousePosChanged(QPointF)), this, SLOT(mousePosChanged(QPointF)));
}

/// with this constructor
EditorView::EditorView(QGraphicsScene* scene, QWidget* parent) : QGraphicsView(scene, parent) {
    setRenderHint(QPainter::Antialiasing);
    setCacheMode(QGraphicsView::CacheBackground);
    setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    setDragMode(QGraphicsView::NoDrag);
    scale(1.0, -1.0); // flip coordinate system so that y increases upwards
    fitInView(-5, -5, 10, 10, Qt::KeepAspectRatio);
    setInteractive(true);
    setBackgroundBrush(QBrush(QColor(232,232,232), Qt::DiagCrossPattern));
}
Community
  • 1
  • 1
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • Scratch that...the scrollbar values are relative to... well it can't be the sceneRect() because those are floats... but something similar. – mpen Aug 31 '09 at 01:36
  • Maybe is how you construct the QGraphicsView, any source code snippet ? – fabrizioM Aug 31 '09 at 02:42
  • I'm not sure what code you want exactly. It doesn't really matter how I construct it... getting the visible rect should be exactly the same. – mpen Aug 31 '09 at 02:50

6 Answers6

30

Just map the pixel-based viewport rectangle to the scene using the view:

graphicsView->mapToScene(graphicsView->viewport()->geometry()).boundingRect()

Bye, Marcel

Marcel Taeumel
  • 596
  • 4
  • 5
8

Nevermind. Came up with this, which seems to work.

QRectF EditorView::visibleRect() {
    QPointF tl(horizontalScrollBar()->value(), verticalScrollBar()->value());
    QPointF br = tl + viewport()->rect().bottomRight();
    QMatrix mat = matrix().inverted();
    return mat.mapRect(QRectF(tl,br));
}
mpen
  • 272,448
  • 266
  • 850
  • 1,236
5

The following implementation returned the best results for me:

QRectF getVisibleRect( QGraphicsView * view )
{
    QPointF A = view->mapToScene( QPoint(0, 0) ); 
    QPointF B = view->mapToScene( QPoint( 
        view->viewport()->width(), 
        view->viewport()->height() ));
    return QRectF( A, B );
}

This works still really well when scrollbars appear. This only works properly if the view does not display the scene rotated or sheared. If the view is rotated or sheared, then the visible rectangle is not axis parallel in the scene coordinate system. In this case

view->mapToScene( view->viewport()->geometry() )

returns a QPolygonF (NOT a QRectF) which is the visible rectangle in scene coordinates. By the way, QPolygonF has a member function boundingRect() which does not return the properly visible rectangle of the view, but might be useful anyways.

Glinka
  • 483
  • 1
  • 3
  • 18
Ralph Tandetzky
  • 22,780
  • 11
  • 73
  • 120
3

here is a possible solution (no clue whether this is the intended one)

QRectF XXX::getCurrrentlyVisibleRegion() const
{
        //to receive the currently visible area, map the widgets bounds to the scene

        QPointF topLeft = mapToScene (0, 0);
        QPointF bottomRight = mapToScene (this->width(), this->height());

        return QRectF (topLeft, bottomRight);
}

HTH, Bernhard

mpen
  • 272,448
  • 266
  • 850
  • 1,236
Bernhard
  • 39
  • 2
  • 1
    In Qt 4.7.4 most answers here don't work. The view coordinates (0, 0) don't seem to always be at the top left of the visible area. Instead the view coordinate system itself is moved by the scrollbars. However Mark's method works! (for me) – Eike Sep 26 '12 at 01:56
2

You can do what you've done, or use the mapToScene() functions. You can't count on the resulting scene "rectangle" being a rectangle, however, because the scene might be rotated or sheared in the view, resulting in a general polygon when mapped to the scene.

If your application never does such things, of course, you're free to assume that a rectangle is always appropriate.

  • Map *what* to the scene though? I can't seem to get the information I need from anything but the scrollbars. – mpen Sep 30 '09 at 01:00
0

It sounds like what you want is the scene rectangle. The ::rect() method is inherited from QWidget. See:

http://doc.qt.io/qt-5/qgraphicsview.html#sceneRect-prop

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
Henrik Hartz
  • 3,677
  • 1
  • 27
  • 28
  • Did you read the description? "The scene rectangle defines the extent of the scene, and in the view's case, this means the area of the scene that you can navigate using the scroll bars." sceneRect returns the *entire* scene, not just the area currently visible (a subsection of the scene). – mpen Sep 06 '09 at 20:23
  • Lifted straight from the docs; "This property holds the area of the scene visualized by this view.". This is what you want right? You can also use mapTo and mapFrom to convert between coordinate systems. – Henrik Hartz Sep 07 '09 at 07:31
  • The docs aren't clear. What does "visualized" mean? Does it mean the area of the scene you are working with or just what is drawn right now? If you have a huge scene you can scroll around, this choice of words is ambiguous. I was also confused. it looks like this is the whole scene, not just the area you can see drawn in the view right now. – Rafe Feb 13 '17 at 22:07
  • The (current) docs are clear if you read on: "The scene rectangle defines the extent of the scene, and in the view's case, this means the area of the scene that you can navigate using the scroll bars." – ypnos Jan 18 '19 at 16:11