- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个自定义 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()
通过返回您的最小尺寸;至
强制固定大小 , 通过返回相同的大小来实现两者。
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/
我是一名优秀的程序员,十分优秀!