隐藏 Qt 小部件并保留小部件空间 [英] Hiding Qt widget and keeping widget space
问题描述
在一个简单的表单中,我制作了几个按钮并放置了一个水平布局.当鼠标离开该区域时,应隐藏其中一个按钮,即最后一个按钮.使用 button->hide()
它可以工作,但是所有按钮都由布局管理器重新排列.我想要的是所有其他按钮都保持在它们的位置.我尝试用小部件占位符替换小部件并交换按钮和占位符 hide()
/show()
,调用 placeholder->resize(button->size())
,但布局管理器不考虑调整大小,并且占位符设置为其最小大小.删除小部件并保留其空间的最佳方法是什么?
In a simple form I made a few buttons and put a horizontal layout. When mouse leaves the area one of the buttons, the last one, should be hidden. Using button->hide()
it works, but all the buttons are rearranged by the layout manager. What I want is that all other buttons remain in their positions. I tried replacing the widget with a widget placeholder and swapping the button and placeholder hide()
/show()
, calling placeholder->resize(button->size())
, but the layout manager doesn't respect the resize, and the placeholder is set with its minimum size. What is the best way to remove a widget and keep its space?
推荐答案
在 Qt 5.2 中可以执行以下操作:
In Qt 5.2 it is possible to do the following:
QSizePolicy sp_retain = widget->sizePolicy();
sp_retain.setRetainSizeWhenHidden(true);
widget->setSizePolicy(sp_retain);
我之前在这里发布了相同的解决方案:
I earlier posted the same solution here:
如何在不改变其他 Qt 小部件位置的情况下使 Qt 小部件不可见?
(这似乎是这个问题的重复).
(which seems to be a duplicate of this question).
这篇关于隐藏 Qt 小部件并保留小部件空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!