gpt4 book ai didi

qt - 如何在子管理调整大小时强制布局更新/调整大小?

转载 作者:行者123 更新时间:2023-12-03 20:15:24 32 4
gpt4 key购买 nike

我有一个自定义 QTextEdit在角落有自己的调整大小的小部件。我可以很好地调整文本小部件的大小,但是它内部的布局不会随着文本编辑的大小而改变。

有没有办法告诉管理布局根据其一个小部件中大小的独立变化重新计算自己?

最佳答案

布局尊重 sizeHint()minimalSizeHint()它的 child (不是他们的 size() )。所以你应该重新实现这两个虚拟方法并返回你想要强制的大小。每当您的尺寸提示发生变化时,您应该调用 updateGeometry() 在您自己的小部件上触发更新放置小部件的布局。
请参阅文档:

  • virtual QSize QWidget::sizeHint() const

    This property holds the recommended size for the widget.
    [...]

  • virtual QSize QWidget::minimumSizeHint() const

    This property holds the recommended minimum size for the widget.
    [...]
    QLayout will never resize a widget to a size smaller than the minimum size hint unless minimumSize() is set or the size policy is set to QSizePolicy::Ignore. If minimumSize() is set, the minimum size hint will be ignored.


所以,到 强制最小尺寸 您的小部件,实现 minimumSizeHint()通过返回您的最小尺寸;至 强制固定大小 , 通过返回相同的大小来实现两者。
我还建议阅读文档页面中的以下部分 "Layout Management" :

Custom Widgets in Layouts

When you make your own widget class, you should also communicate its layout properties. If the widget has a one of Qt's layouts, this is already taken care of. If the widget does not have any child widgets, or uses manual layout, you can change the behavior of the widget using any or all of the following mechanisms:

  • Reimplement QWidget::sizeHint() to return the preferred size of the widget.
  • Reimplement QWidget::minimumSizeHint() to return the smallest size the widget can have.
  • Call QWidget::setSizePolicy() to specify the space requirements of the widget.

Call QWidget::updateGeometry() whenever the size hint, minimum size hint or size policy changes. This will cause a layout recalculation. Multiple consecutive calls to QWidget::updateGeometry() will only cause one layout recalculation.



更新(见评论):
您还可以通过调用强制大小(或仅高度/宽度):
QWidget::setFixedSize(QSize);
QWidget::setFixedHeight(int);
QWidget::setFixedWidth(int);
这些方法只是将大小提示设置为给定值并调用 updateGeometry() AFAIK。但也许他们做得更多,因为(如评论中所见)这是我能找到解决问题的唯一方法。

关于qt - 如何在子管理调整大小时强制布局更新/调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10758267/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com