Resize a QWidget containing a QVBoxLayout in runtime when contents are partially Hidden

谁说胖子不能爱 提交于 2019-12-13 00:31:58

问题


In this application, a list of HBoxLayouts is generated and put into a VBoxLayout in order to form a dynamically filled list of commands. There is a button above each sub-list which has the capability to hide the controls below it. The problem: when a sub-list is hidden, the widget which contains the broadest VBoxLayout does not change in size! The VBoxLayout then stretches to compensate. I want the container widget to shrink when it contains fewer items! It looks like this:

The problem is, when I hide a list under one of the PushButtons, the containing widget won't shrink! the VBoxLayout just expands when there are less visible items :(

Here is a picture of how I see my little GUI.

Here is some code from the broader layout setup:

scrollArea = new QScrollArea(this);
scrollAreaLayoutWidget = new QWidget(scrollArea);
scrollAreaLayout = new QVBoxLayout(scrollAreaLayoutWidget);
//scrollAreaLayout is filled with all the stuff you see inside the scroll area...
scrollArea->setWidget(scrollAreaLayoutWidget);

I have tried changing the QSizePolicy of the container widget (blue box) in a number of ways. I've found dozens of resizing questions but I have somehow not discovered an answer. Is there a magic auto-resize setting that I'm missing? Let me know if this is painfully vague or incomplete. Any comment is welcome!


回答1:


Turns out all I had to do was create a custom SLOT for when the button that hides part of the Ui is clicked and hide the relevant widget within that function. In the same function, I called [containerWidget]->adjustSize(); and it did exactly what I was looking for.



来源:https://stackoverflow.com/questions/11746958/resize-a-qwidget-containing-a-qvboxlayout-in-runtime-when-contents-are-partially

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