如何使 Qt 小部件改变其大小?
我的问题可能很简单,但我不知道如何解决。
所以,我有几个垂直布局的小部件。其中一些在某个时刻应该是看不见的。我认为此时已经扩大垂直政策的控制应该增加其高度,但它从未发生过。为什么?如何强制它改变大小?到目前为止,只有一种方法可以做到这一点 - 手动稍微更改窗口的大小,并且仅在窗口小部件更改其高度之后。
My problem probably is very simple, but I have no idea how to solve it.
So, I have several widgets in vertical layout. Some of them in some moment should be invisible. I suppose this moment the control that has expanding vertical policy should increase its height but it never happens. Why? How to force it to change the size? So far there is only one way to do that - to change the size of window manually a little bit and only after it the widget changes its height.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您使小部件不可见(即调用 隐藏)时,也将其删除从布局(请参阅 removeWidget)。这样,所有其他小部件将自动调整大小以利用额外的空间。由于您只是隐藏了小部件,因此它的空间仍然保留在垂直布局中。
使用 insertWidget 在将隐藏的小部件转换回来时保持布局顺序进入视野。否则, addWidget 总是将其放置在底部。这种方法应该比手动管理尺寸简单。
When you make a widget invisible (i.e. call hide), also remove it from the layout (see removeWidget). That way all of the other widgets will automatically resize to make use of the extra space. Since you are only hiding the widget, its space is still reserved in the vertical layout.
Make use of insertWidget to maintain the layout ordering when transitioning a hidden widget back into view. Otherwise, addWidget would always place it at the bottom. This approach should be less complex than managing the sizes manually.
尝试在布局的父级上调用
adjustSize()
。Try calling
adjustSize()
on the parent of the layout.您是否尝试过将要在其他部件隐藏时展开的部件的拉伸设置为 1?
当这个小部件添加拉伸为 1 且其他小部件没有拉伸时,该小部件将扩展到可用空间。也许这会起作用。
Have you tried setting the stretch on 1 for the widget you want to be expanded when others are hidden?
When this widget is added with a stretch of 1 and the other widgets without stretch, this widget will expand to the available space. Maybe that will do the trick.