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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With