QWidget reports wrong width value

南笙酒味 提交于 2019-12-10 18:49:11

问题


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

EDIT:

int myWidth = this->width() returns the same 640


回答1:


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.



来源:https://stackoverflow.com/questions/14943715/qwidget-reports-wrong-width-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!