views:

855

answers:

4

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));
}
A: 

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));
}
Mark
+1  A: 

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

http://doc.qt.nokia.com/4.6-snapshot/qgraphicsview.html#sceneRect-prop

Henrik Hartz
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).
Mark
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
A: 

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.

SixDegrees
Map *what* to the scene though? I can't seem to get the information I need from anything but the scrollbars.
Mark
+2  A: 

Hi,

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

Bernhard