QGridLayout, 3 panes, not expanding properly

社会主义新天地 提交于 2019-12-05 06:53:37
Kaleb Pederson

Look at the docs on QGridLayout::AddWidget:

The default alignment is 0, which means that the widget fills the entire cell.

But you have the following:

gridLayout->addWidget(list, 1, 0, Qt::AlignLeft);
gridLayout->addWidget(flashList, 1, 1, Qt::AlignCenter);
gridLayout->addWidget(infoButton, 0, 3, Qt::AlignRight);
gridLayout->addWidget(flashFeedsButton, 0, 1, Qt::AlignLeft);

Thus, you've specifically told the grid layout that you want your widget aligned to the left, center, right, and left of their cells, respectively. In your case you probably want to use the default alignment allowing the widgets to fill the cells and follow their own respective size policies.

It should just be a matter of setting the proper size policy on your widgets. So the buttons on the left and right:

button->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );

and for the middle button

button->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );

where the first argument defines the horizontal size policy and the second one defines the vertical size policy.

See the size policy documentation as well

You have a lot of variables going here (different widgets, different size policies on widgets, and size policies working on the layout itself, so it's a bit difficult to know where things are going wrong. A couple of suggestions:

1) First try to accomplish what you want with just one type of widget. For example, all QLabels with all Expanding size policies. 2) Only adjust the size policies of the widgets, I found that adding stuff in the grid layout starts making things confusing. The stretch factor for the widget's size policy should work fine. 3) Don't be afraid to try different ratios for stretch factors.

I've found that this is one of the things where Qt Designer (Creator) is helpful for puzzling these things out. It's much faster to adjust things in Designer and resize then the run compile cycle. After you have solved the issue there, you can take the properties you have found and put it in your code instead of a edit, compile, run.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!