简体   繁体   English

Qt Widget包含空白布局时会自动调整大小

[英]Qt Widget automatically resize if it contains empty layout

I have been given a qt widget containing some elements and layouts. 我得到了一个包含一些元素和布局的qt小部件。 I am trying to add an empty layout to this main widget (My plan is to populate it with a widget in the future). 我正在尝试向此主窗口小部件添加一个空布局(我的计划是将来使用窗口小部件填充它)。 I am expecting the main widget to resize automatically if the layout is empty. 我希望如果布局为空,则主窗口小部件会自动调整大小。 How can I do that? 我怎样才能做到这一点? I tried to play on the size policies of the widget but I did not get much results so far . 我尝试使用小部件的大小策略,但到目前为止没有得到太多结果。

In the end the idea is to get the main widget (which is not contained in a layout by the way) to resize automatically if it contains an empty layout in itself 最后的想法是让主窗口小部件(它本身不包含在布局中)在其自身包含空布局时自动调整大小

I think, you can look for adjustSize method of QWidget . 我认为,您可以寻找QWidget AdjustSize方法。 When you add or remove any internal widget in layout, you can make something like this: 在布局中添加或删除任何内部窗口小部件时,您可以进行如下操作:

QTimer::singleShot(0, this, SLOT(slotAdjustSize()));

...

void YourWidget::slotAdjustSize()
{
    adjustSize();
}

Size of your widget will be fit to its contents. 小部件的大小将适合其内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM