scrollbars and qgraphicsview
I have a problem with setting the maximumsize of a qgraphicsview.
I do not want any background canvas area to be visible in the qgraphicsview, so when the window is sized sufficiently large, it should be as in below picture (except without scrollbars.)
For smaller window sizes, the view can be smaller and scrollbars are required.
The problem is, I cannot get the scrollbars to disappear for a maxed window, where the maximum qgraphicsview size is limited to the views bounding rectangle:
http://www.qtcentre.org/forum/pictur...5&pictureid=26
Code:
{
QIcon OpenIcon
= this
->style
()->standardIcon
(QStyle::SP_DirOpenIcon,
0,
this);
QMenu *file = this
->menuBar
()->addMenu
("&File");
file->addAction(open);
file->addAction(quit);
QToolBar *toolbar
= addToolBar
("main toolbar");
QAction *open2
= toolbar
->addAction
(OpenIcon,
"Open File");
toolbar->addSeparator();
scene
->addText
("Hello, world!",
QFont("Helvetica [Cronyx]",
100));
scene->itemsBoundingRect();
view->setContentsMargins ( 0,0,0,0 );
view->setMaximumSize(view->sceneRect().width()+1,view->sceneRect().height()+1);
//view->setMaximumSize(view->maximumViewportSize()); /* also does not work*/
this->setCentralWidget(view);
this->resize(800,300);
}
Re: scrollbars and qgraphicsview
Hi,
Take a look at QAbstractScrollArea(note that QGraphicsView inherits it):
setVerticalScrollBarPolicy
setHorizontalScrollBarPolicy
You can set them to "Qt::ScrollBarAlwaysOff" or "Qt::ScrollBarAlwaysOn". So you have to know when you want to show or hide them.
Re: scrollbars and qgraphicsview
Thanks ^NyAw^ .
I indeed checked this out, however the default is a mode where it hides the unneeded scrollbars. I only wonder why this command does not function:
view->setMaximumSize(view->sceneRect().width()+1,view->sceneRect().height()+1);
because if I add a margin of say 30 pixels, it will autohide unneeded scrollbars. But the graphicsview is too large in that case (I want the view showing a pixmap, without anything else).
I wonder which borders I'm missing here, or something else I'm sizing wrong? :confused:
Re: scrollbars and qgraphicsview
Hi,
I think that the scrollBars appear because the scene is larger than the view, so you are defining the maximum size of the view but the scene is larger so the scrollBars apear.
Try resizing the scene to the view size.
Re: scrollbars and qgraphicsview
Ok, I tried that, in the docs of qgraphicsview:
If unset, or if set to a null QRectF, sceneRect() will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks).
So I added:
Code:
view
->setSceneRect
(QRect());
But unfortunately, to no avail...
Re: scrollbars and qgraphicsview
Hi,
may be a stupid question, but you are setting a maximum size which does not mean that the size of the view is set to it. So to be sure also call
Code:
view->setContentsMargins ( 0,0,0,0 );
view->setMaximumSize(view->sceneRect().width()+1,view->sceneRect().height()+1);
view->resize(view->maximumSize());
Lykurg
Re: scrollbars and qgraphicsview
Ok, just tried that as well. Unfortunately it doesn't work.
I think the size is sufficiently large, because i set the maximum size of the widget to be 1 pixel larger than the view. And the view has the same size as the scene.
Re: scrollbars and qgraphicsview
Ha, one last thought, sometime the fonts are buggy related to their bounding boxes. Did you try different fonts with the same result?
Lykurg
Re: scrollbars and qgraphicsview
You can try calling QGraphicsView::fitInView based on you main window size. This will ensure all contents of scene will be visible.
Re: scrollbars and qgraphicsview
Thanks, also tried that, unfortunately it also does not work.
I gave up on the graphicsview, instead trying to modify the qt image viewer example and there I see the same problem. If i set maximumsize, scrollbars appear where they should not !:crying:
Does anyone have an example of a qscrollarea or qgraphicsview in a window, where the window cannot be stretched further AND the scrollbars disappear, if the view is filled completely. The layout management of qt is some kind of voodoo to me.
Im now starting to become completely desperate, this cannot be impossible???
Re: scrollbars and qgraphicsview
Re: scrollbars and qgraphicsview
This must be implemented in many qt programs, nobody with a hint on how to tackle this?
Re: scrollbars and qgraphicsview
Äh, sorry, my comment was capable of being misunderstood. I meant forget my post, not your issue...:o
Re: scrollbars and qgraphicsview
Ah, I figured out the scrollbar problem at least:
I set a maximum size on the QScrollArea / QGraphicsView widget, but that size should include the widget borders (which are exactly 2 px). When I add exactly 4 pix to the maximumsize (2px at each side) the scrollarea is exactly the right size, without scrollbars.
This is probably due to the fact that the widget inherits from QFrame, the frame is removable by setting the frameShape of the widget to NoFrame. (currently testing) Better would be some way to query the border size, or to set the size within the borders, but I don't know how to do that.
Thanks for all the help!