QWidget 报告错误的宽度值 [英] QWidget reports wrong width value
问题描述
我想查询小部件的宽度以进行一些自定义布局管理.不幸的是,无论小部件实际上有多大或多小,以下代码都会返回 640
I want to query a widget's width to carry out some custom layout management. Unfortunately, the following code returns 640 no matter how big or small the widget actually is
int myWidth = this->rect().size().width(); // "this" is my class derived from QWidget
// myWidth is set to 640
int myWidth = this->width()
返回相同的 640
推荐答案
覆盖特殊布局管理的正确位置 resizeEvent
.那时小部件的大小决定了(通常由父小部件的布局决定,基于 尺寸提示 和 尺寸政策).它也保证在第一次显示小部件之前被调用.记得调用超类 resizeEvent
同样,至少如果您在布局中有子小部件.
Correct place to do special layout management is overridden resizeEvent
. At that point size of widget is decided (usually by parent widget's layout, based on size hint and size policy). It's guaranteed to get called before widget is shown for the first time, too. Remember to call super class resizeEvent
too, at least if you have child widgets in layout.
这篇关于QWidget 报告错误的宽度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!