隐藏Qt小部件并保留小部件空间 [英] Hiding Qt widget and keeping widget space

查看:701
本文介绍了隐藏Qt小部件并保留小部件空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以简单的形式做了几个按钮,并放置了水平布局.当鼠标离开该区域时,应隐藏最后一个按钮.使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆