隐藏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:
(which seems to be a duplicate of this question).
这篇关于隐藏Qt小部件并保留小部件空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!