问题
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