PDA

View Full Version : How to enlarge sceneRect() to the viewport size



iw2nhl
27th August 2007, 04:15
Hi,
when you create a QGraphicsView with some items and you move one of them, the others move in the opposite direction.
This is not very nice, so I'm trying to avoid this enlarging the sceneRect() to the size of the viewport.
The code should enlarge it if needed, else it should be set to itemsBoundingRect().

I tried to do it, but when I move an item, it starts jumping around!
It seems there is an offset problem.
Anyone else tried to achieve this?

Here is code:


QGraphicsScene *m_drawScene;
QGraphicsView *m_drawView;

// ...

qreal xBorder = 0.0;
qreal yBorder = 0.0;
QRectF sceneRect = m_drawScene->itemsBoundingRect();
const QWidget *drawArea = m_drawView->viewport();

QPoint topLeft = m_drawView->mapFromScene(sceneRect.topLeft());
QPoint bottomRight = m_drawView->mapFromScene(sceneRect.bottomRight());

const int windowWidth = drawArea->width();
const int sceneWidth = bottomRight.x() - topLeft.x();
if (sceneWidth < windowWidth) {
xBorder = (windowWidth - sceneWidth) / 2.0;
}

const int windowHeight = drawArea->height();
const int sceneHeight = bottomRight.y() - topLeft.y();
if (sceneHeight < windowHeight) {
yBorder = (windowHeight - sceneHeight) / 2.0;
}

sceneRect.adjust(-xBorder, -yBorder, xBorder, yBorder);
m_drawScene->setSceneRect(sceneRect);


Thanks for any help, I really can't find a solution :-(

Bitto
27th August 2007, 12:14
The reason this happens is that you haven't defined a scene rect. QGraphicsScene will assume the scene is as large as the bounding rect of all your items, and QGraphicsView auto-centers the scene in the view. When moving an item, it'll appear that the other items move in the opposite direction until the scene is so large that you need scrollbars. Then, the auto-centering stops.

Setting a scene rect stops this right away. QGraphicsScene has a scene rect in its constructor, or you can call QGraphicsScene::setSceneRect().

iw2nhl
27th August 2007, 16:25
Thanks for your answer!
I think to know what is happening, but I don't know how to avoid it.
You say:

When moving an item, it'll appear that the other items move in the opposite direction until the scene is so large that you need scrollbars. Then, the auto-centering stops.
How to find the needed size so that auto-centering does not happen?
My idea was to set the scene rect size equal to the viewport size, but probably I'm following the wrong way...
I'll do some more tries!

iw2nhl
27th August 2007, 16:39
Here is another try, but also this one does not work :-(


QRectF boundingRect = m_drawScene->itemsBoundingRect();
const QWidget *viewport = m_drawView->viewport();
QRect viewportRect = viewport->geometry();

QPoint topLeft = m_drawView->mapFromScene(boundingRect.topLeft());
QPoint bottomRight = m_drawView->mapFromScene(boundingRect.bottomRight());

QRect mappedBoundingRect = QRect(topLeft, bottomRight);
QRect sceneRect = viewportRect.united(mappedBoundingRect);
m_drawScene->setSceneRect(sceneRect);