简体   繁体   English

Qt QWidget :: minimumSizeHint延迟(#2)

[英]Qt QWidget::minimumSizeHint delay (#2)

When i hide a widget in my application, the minimumSizeHint isn't updated immediatly. 当我在应用程序中隐藏窗口小部件时, minimumSizeHint不会立即更新。

I tried the QLayout::activate() solution suggested at this post , but it doesn't work for me because QLayout::activate() returns false . 我试过QLayout::activate()的解决方案,在提出这个职位 ,但它不适合我,因为QLayout::activate()返回false

I try to do it at MainWindow class like this: 我尝试在MainWindow类上这样做:

ui.groupBox->setVisible(!ui.groupBox->isVisible());

qDebug() << this->layout()->activate();

qDebug() << this->minimumSizeHint();

this->resize(this->minimumSizeHint());

Any ideas why it's not working? 任何想法为什么它不起作用?

My current workaround is: 我当前的解决方法是:

QTimer::singleShot(10, this, SLOT(on_resizeMin()));

but i noticed 10ms may not be enough on a slow system. 但我注意到10ms在慢速系统上可能还不够。 Nasty workaround. 讨厌的解决方法。

The size hints are meant to be used at appropriate moments by the layout system. 尺寸提示应在布局系统的适当时候使用。 You're not supposed to be using them elsewhere - that's why they appear to "not work". 您不应该在其他地方使用它们-这就是为什么它们似乎“不起作用”的原因。

You appear to want to constrain the size of the main widget (the window) to one of the size hints. 您似乎想将主窗口小部件(窗口)的大小限制为大小提示之一。 You'll need to set an appropriate sizeConstraint on that widget's layout. 您需要在该小部件的布局上设置适当的sizeConstraint There are two approaches: 有两种方法:

  1. Use the QLayout::SetFixedSize constraint. 使用QLayout::SetFixedSize约束。 Reimplement the layout's sizeHint to return minimumSize , such that the QLayout::SetFixedSize will set the window's size to the minimum size hint, not the default size hint. 重新实现布局的sizeHint以返回minimumSize ,这样QLayout::SetFixedSize会将窗口的大小设置为最小大小提示,而不是默认大小提示。

  2. Patch Qt to implement QLayout::SetMinAndMaxToMinSize , meaning "The main widget's minimum and maximum size are set to minimumSize() " 修补Qt以实现QLayout::SetMinAndMaxToMinSize ,这意味着“将主窗口小部件的最小最大大小设置为minimumSize()

    The patch is small :) 补丁很小:)

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

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