PDA

View Full Version : QGraphicsScene Scrollbar resizing



ikeurb
10th July 2009, 00:01
I have a QGraphicsView class with a QGraphicsScene.

In the scene I add items, much like in a ListView. When the number of items exceed what will fit in the view, scrollbars appear much like they should. Behavior is exactly what I want at this point.

When items are removed from the scene so that all of the remaining items are visible in the list, the scrollbars are still there.

Shouldn't the scrollbars disappear when not needed? I can use the scrollbars to scroll to the bottom of the scene (where the scene was previosly pupulated with items), but there are no items at the bottom anymore because they were previously removed, so the scene should resize and the scrolbars disappear.



for (int index = 0; i <= itemsInList(); index++)
{
m_scene->addItem( myItem[index]) ;
}

for (int index = 0; i <= itemsInList(); index++)
{
if ( myItem[index]->isSelected() )
{
m_scene->removeItem( myItem[index] );
}
}

shentian
10th July 2009, 10:19
The need of scrollbars of a QGraphicsView depends on QGraphicsScene::sceneRect. The sceneRect grows but never shrinks automatically. You have to adjust it manually, for example with:


m_scene->setSceneRect(m_scene->itemsBoundingRect());

ikeurb
10th July 2009, 17:10
Great! That was it. I knew it had to be something simple that I wasn't doing.

THANKS!

ikeurb
10th July 2009, 18:57
Aagh, now there is a new issue.

The scene does resize correctly as items are added and removed to the scene. This is good.

However, if ALL of the items are removed from the scene, the scrollbars then reappear.

With the scene empty, there should not be any scrollbars.

???

shentian
10th July 2009, 23:12
Thanks, I never checked this case in my app, didn't work there either.

I there are no items, QGraphicsScene::itemsBoundingRect returns the null QRectF. But QGraphicsScene::setSceneRect ignores the null QRectF. This should work:



QRectF rect = m_scene->itemsBoundingRect();
if (rect.isNull())
{
m_scene->setSceneRect(QRectF(0, 0, 1, 1));
}
else
{
m_scene->setSceneRect(rect);
}

ikeurb
14th July 2009, 16:33
That did the trick. Simple enough!

Thanks again.