Get visible rectangle of QGraphicsView?
Asked Answered
P

7

19

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));
}
Pinkiepinkish answered 31/8, 2009 at 1:32 Comment(3)
Scratch that...the scrollbar values are relative to... well it can't be the sceneRect() because those are floats... but something similar.Pinkiepinkish
Maybe is how you construct the QGraphicsView, any source code snippet ?Casefy
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.Pinkiepinkish
P
9

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));
}
Pinkiepinkish answered 31/8, 2009 at 5:30 Comment(1)
Thanks, works. See my reply for a Qt5/Qt6 version of this code (can't write multi-line code in a comment).Seychelles
I
29

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

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

Bye, Marcel

Inpour answered 23/1, 2011 at 18:46 Comment(4)
Might be shorter to use graphicsView->rect() instead of graphicsView->viewport()->geometry().Uganda
@Uganda The viewport() is relevant if there are borders around it (or visible scrollbars).Economics
Not sure this is correct. graphicsView->mapToScene() takes coordinates relative to the viewport, not coordinates of the viewport relative to the graphicsview.Seychelles
In practice it almost works, but it's slightly too big, it includes the area under the scrollbars, which isn't technically visible.Seychelles
P
9

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));
}
Pinkiepinkish answered 31/8, 2009 at 5:30 Comment(1)
Thanks, works. See my reply for a Qt5/Qt6 version of this code (can't write multi-line code in a comment).Seychelles
R
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.

Ronnironnica answered 1/6, 2012 at 11:40 Comment(0)
B
3
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);
}
Buhler answered 29/7, 2010 at 9:28 Comment(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)Countersignature
E
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.

Excess answered 29/9, 2009 at 10:35 Comment(1)
Map what to the scene though? I can't seem to get the information I need from anything but the scrollbars.Pinkiepinkish
S
1

Qt5/Qt6 version of the solution by @mpen:

QRectF EditorView::visibleRect() {
    const QRect viewportRect(QPoint(horizontalScrollBar()->value(), verticalScrollBar()->value()), viewport()->size());
    const auto mat = transform().inverted();
    return mat.mapRect(viewportRect);
}
Seychelles answered 9/4 at 9:38 Comment(0)
B
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

Butane answered 6/9, 2009 at 9:2 Comment(4)
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).Pinkiepinkish
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.Butane
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.Mischance
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."Chronic

© 2022 - 2024 — McMap. All rights reserved.