QGraphicsScene Scrollbar resizing
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.
Code:
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] );
}
}
Re: QGraphicsScene Scrollbar resizing
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:
Code:
m_scene->setSceneRect(m_scene->itemsBoundingRect());
Re: QGraphicsScene Scrollbar resizing
Great! That was it. I knew it had to be something simple that I wasn't doing.
THANKS!
Re: QGraphicsScene Scrollbar resizing
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.
???
Re: QGraphicsScene Scrollbar resizing
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:
Code:
QRectF rect
= m_scene
->itemsBoundingRect
();
if (rect.isNull())
{
m_scene
->setSceneRect
(QRectF(0,
0,
1,
1));
}
else
{
m_scene->setSceneRect(rect);
}
Re: QGraphicsScene Scrollbar resizing
That did the trick. Simple enough!
Thanks again.