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:
// ...
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);
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);
To copy to clipboard, switch view to plain text mode
Thanks for any help, I really can't find a solution :-(
Bookmarks