PDA

View Full Version : qgraphicsview and qsplitter sizing problem



hayzel
2nd February 2015, 07:28
I make a program with a center view and a QStackedwidget on the left that keeps properties. The stacked widget contains for now 4-5 widgets designed in qt designer.
I recently added a new page in the stacked widget that contains some qwidgets and a QGraphicsView:
10934
As you can see the top QGraphicsView works and resizes fine inside qt designer.
When I add the above qwidget to the left properties stackedwidget though, my qgraphicsview resizes awfully and gives me the below pic:
10935
The QGraphicsView gets too big vertically destroying the size of my main window and extents outside the screen.

I read some posts about qgraphicsview behaviour inside a qsplitter, and the need to manipulate stretch factor when the scene of the qgraphicsview changes, but I could not understand how to do it.

Any ideas how to solve this issue? Notice that if I remove the splitters then all the layouting is fine. But I liked the notion to let the user resize with a splitter the view/properties ratio.

d_stranz
2nd February 2015, 21:44
Are you using a layout on the stack widget page that contains the splitter? Show the code where you create this page. It looks to me as though you haven't put the widget that contains the splitter into a layout, and as a result, the view is expanding vertically to whatever size you have set for the graphics view.

hayzel
3rd February 2015, 07:10
My main app window has a central QWidget consisting of a QGraphicsView and a QStackedWidget:


m_view= new saView(this);
m_stack=new QStackedWidget();
m_stack->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Mi nimumExpanding);
m_stack->setMinimumWidth(256);
....
initWidgets();//this is the place where m_widgets[] holds the pointers of the new properties widgets
for(i=0;i<5;i++) {m_stack->addWidget(m_widgets[i]);};
QGridLayout *layout = new QGridLayout;//here I tried QHBoxLayout or QVBoxLayout but nothing....
QSplitter *splitter=new QSplitter(this);
splitter->setChildrenCollapsible(false);
splitter->addWidget(m_view);
splitter->addWidget(m_stack);
layout->addWidget(splitter);
setLayout(layout);
....


The initWidgets() function :


void saGui::initWidgets()
{
//qDebug()<<"saGui::initWidgets";
int i;
//the 0 index is the empty properties window
m_widgets[0]=new QWidget();

//1 index is SteelNodeProperties window
saWidgetSteelNodeProperties* w=new saWidgetSteelNodeProperties(this);
m_widgets[1]=w;
...
...

// 2 index is ConcretePolygonProperties window
saWidgetConcretePolygonProperties* w2=new saWidgetConcretePolygonProperties(this);
m_widgets[2]=w2;
...

// 3 index is SteelLineProperties window
saWidgetSteelLineProperties* w3=new saWidgetSteelLineProperties(this);
m_widgets[3]=w3;
....

//4 index is the QWidget that contains the QGraphicsView that gets resized wrongfully
saWidgetMaterialProperties* w4=new saWidgetMaterialProperties(&m_materials,this);
m_widgets[4]=w4;//adding w4 to m_widgets produces the wrong size.

};


The QWidget is made inside Qt Designer. It has a QVBoxLayout, containing a QSplitter of a QGraphicsView, and a QStackedWidget , and a VerticalSpacer .
The file of the QWidget is here: 10937.

Notice that if I don't do :


scene->setSceneRect(....);

in my code , and just use the QGraphicsView/Scene adding items. the QGraphicsView starts with those sizes:
10938
,that looks right but it is not. Because QGraphicsView has small height. And the vertical splitter between them cannot be moved. But If I put some items inside QGraphicsScene that are outsize active view the QGraphicsView starts to grows its size, growing also the mainApp window size and moving the window contents out of the screen .

hayzel
5th February 2015, 07:50
Found out a solution. I don't know why though. The problem is fixed by trial and error.
Firstly on the central view/properties splitter, I used QSizePolicy::Ignored :


m_view= new saView(this);
m_stack=new QStackedWidget();
m_stack->setSizePolicy(QSizePolicy::Preferred,QSizePolicy:: Ignored);
m_stack->setMinimumWidth(256);
....
initWidgets();//this is the place where m_widgets[] holds the pointers of the new properties widgets
for(i=0;i<5;i++) {m_stack->addWidget(m_widgets[i]);};
QGridLayout *layout = new QGridLayout;//here I tried QHBoxLayout or QVBoxLayout but nothing....
QSplitter *splitter=new QSplitter(this);
splitter->setChildrenCollapsible(false);
splitter->addWidget(m_view);
splitter->addWidget(m_stack);
QList<int> sizes;sizes<<600<<100;
splitter->setSizes(sizes);
layout->addWidget(splitter);
setLayout(layout);
....

The first Ignored flag keeps the m_stack (properties) widget inside the main window, and does not resize it outside the screen.
The setSizes forces the properties widget to its minimum size.

As for the vertical splitter inside the view, I used also inside qt designer for the QGraphicsView (top of the vertical splitter) the Ignored flag, to make it resizeble.
And set the ratio of view/other properties with :


ui.splitter->setStretchFactor(0,1);
ui.splitter->setStretchFactor(1,3);

inside the constructor of the widget page.

PS. I cannot find how to change the thread name to SOLVED ,