Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QWidget reports wrong width value

Tags:

qt

qwidget

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

like image 454
S B Avatar asked Feb 18 '13 19:02

S B


1 Answers

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.

like image 112
hyde Avatar answered Oct 19 '22 19:10

hyde