PDA

View Full Version : QGraphicsView Horizontal Stretch in a QSplitter



bassPenguin
9th April 2012, 07:26
I am designing a simple GUI that includes a QSplitter containing a QListWidget and a QGraphicsView coupled horizontally. I want the list widget on the left side occupying 20% of the splitter container and the graphics view on the right side occupying 80% of the splitter. I want the user to be able to adjust these ratios by dragging the splitter bar, so I cannot fix these values. Here is approximately how I want it to look:
7569

It actually works fine when I set the horizontal stretches to 20 and 80 (as seen in the above picture) until I set the graphics view scene with QGraphicsView::setScene(). Then the splitter bar moves over to the right side of the window like this:
7570

It seems to give the QGraphicsView widget the minimum amount of space allowed in the splitter. I tried setting the horizontal stretch again after setting the scene like this:

ui.graphicsView->setScene(scene);
ui.graphicsView->sizePolicy().setHorizontalStretch(80);
but it does not seem to make a difference. How can I avoid this resizing when setting the scene? Thanks.

bassPenguin
9th April 2012, 13:53
I resolved the problem.

I tried setting everything up in code instead of using Qt Designer and it worked as expected. Finally, I realized that I must have done something wrong in Qt Designer and went back to check. Turns out that Designer hides away the central widget in the window layout, so I had actually been putting the layout on the window the whole time instead of the central widget. I had to select the central widget in the tree view on the side and then set the layout from the top bar menu (instead of context menu). Now it all seems to work :)

bassPenguin
10th April 2012, 02:46
I actually did not resolve my problem. It looks like I misunderstood my own solution. I had accidentally removed the splitter (which then does not allow the user to resize). After looking into a little more, the problem seems to be a known issue when splitters and layouts interact. Here is some relevant information for anyone else who runs into the same problem:

http://www.qtcentre.org/threads/44401-My-Qgraphicsview-widget-is-not-resized-by-the-vertical-splitter-which-has-control

Spitfire
10th April 2012, 17:06
Couple of things:

1) Code you've posted at the beggining is just wrong, you change stretch factor on a copy of the size policy! you have to do it this way:


QSizePolicy policy = widget->sizePolicy();
policy.setHorizontalStretch(stretch);
policy.setVerticalStretch(stretch);
widget->setSizePolicy(policy);


2) stretch factor is probably not entirely what you think, from docs:


void QSplitter::setStretchFactor ( int index, int stretch )
Updates the size policy of the widget at position index to have a stretch factor of stretch.

stretch is not the effective stretch factor; the effective stretch factor is calculated by taking the initial size of the widget and multiplying it with stretch.
So setting factors of 20 and 80 will not result in 20%/80% split.

3) Minimum size of graphics view depends on the scene rectangle. If you don't set any then the view will be always squizes to smallest possible regardless of stretch factor.


QGraphicsScene* scene = new QGraphicsScene( this );
scene->setSceneRect( 0, 0, 500, 500 ); // this is what you need
QGraphicsView* view = new QGraphicsView();
QListWidget* list = new QListWidget();

view->setScene( scene );
scene->addEllipse( 50, 50, 40, 40, QPen( Qt::red ), QBrush( Qt::blue ) );

QSplitter* sp = new QSplitter( this );
sp->addWidget( list );
sp->addWidget( view );

sp->setStretchFactor( 0, 1 );
sp->setStretchFactor( 1, 2 );

this->setCentralWidget( sp );

Code above creates a spliter with list and view in (more or less) 20%/80% initial split. Changing scene rect will require chaning stretch factor to keep 20/80 ratio.
BTW setSize() doesn't work when it comes to setting initial widgets sizes. Never figured it out why though.

bassPenguin
12th April 2012, 07:05
Thanks Spitfire! Good catch on the code. It took me too long to see that mistake :p. Thanks for the clarification on QSplitter::setStretchFactor(). I did actually get what I was looking for using QSplitter::setSizes(), but I have only tested it in Windows. If I have trouble later, I will try setting the scene rectangle as you suggested.