PDA

View Full Version : scrollbars and qgraphicsview



captiva
17th February 2009, 14:09
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/picture.php?albumid=5&pictureid=26


MyMain::MyMain(QWidget *parent):QMainWindow(parent)
{
QIcon OpenIcon = this->style()->standardIcon(QStyle::SP_DirOpenIcon, 0, this);

QAction *quit = new QAction("&Quit", this);
QAction *open = new QAction("&Open", 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 = new QGraphicsScene();
scene->addText("Hello, world!",QFont("Helvetica [Cronyx]", 100));
view = new QGraphicsView(scene, this);
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);
}

^NyAw^
17th February 2009, 15:30
Hi,

Take a look at QAbstractScrollArea(note that QGraphicsView inherits it):

setVerticalScrollBarPolicy (file:///C:/Qt/4.3.0/doc/html/qabstractscrollarea.html#verticalScrollBarPolicy-prop)
setHorizontalScrollBarPolicy (file:///C:/Qt/4.3.0/doc/html/qabstractscrollarea.html#horizontalScrollBarPolicy-prop)

You can set them to "Qt::ScrollBarAlwaysOff" or "Qt::ScrollBarAlwaysOn". So you have to know when you want to show or hide them.

captiva
17th February 2009, 16:26
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:

^NyAw^
17th February 2009, 16:45
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.

captiva
17th February 2009, 16:59
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:


view->setSceneRect(QRect());

But unfortunately, to no avail...

Lykurg
17th February 2009, 17:19
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



view->setContentsMargins ( 0,0,0,0 );
view->setMaximumSize(view->sceneRect().width()+1,view->sceneRect().height()+1);
view->resize(view->maximumSize());


Lykurg

captiva
17th February 2009, 17:40
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.

Lykurg
17th February 2009, 18:08
Ha, one last thought, sometime the fonts are buggy related to their bounding boxes. Did you try different fonts with the same result?

Lykurg

aamer4yu
17th February 2009, 18:20
You can try calling QGraphicsView::fitInView based on you main window size. This will ensure all contents of scene will be visible.

captiva
19th February 2009, 10:16
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???

Lykurg
19th February 2009, 10:27
EDIT: forget it...

captiva
19th February 2009, 10:44
This must be implemented in many qt programs, nobody with a hint on how to tackle this?

Lykurg
19th February 2009, 11:25
Äh, sorry, my comment was capable of being misunderstood. I meant forget my post, not your issue...:o

captiva
20th February 2009, 12:00
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!