Can't get geometry() in form constructor
I've QGridLayout on my widget and I need to add several elements to it during runtime.
Everything is fine, but I need to calculate and store proper height for added elements. I've tried to get it through
Code:
ui->setupUi(this);
...
createRuler(minValue, maxValue, steps);
in createRuler I've tried this :
Code:
int frame_height = ui->rulerLayout->geometry().height() / steps;
But it allways return 0; However inside setupUi there is a code :
Code:
gridLayoutWidget
= new QWidget(rulerFrame
);
gridLayoutWidget
->setObjectName
(QString::fromUtf8("gridLayoutWidget"));
gridLayoutWidget
->setGeometry
(QRect(0,
0,
61,
361));
rulerLayout->setSpacing(6);
rulerLayout->setMargin(11);
rulerLayout
->setObjectName
(QString::fromUtf8("rulerLayout"));
rulerLayout
->setSizeConstraint
(QLayout::SetMinAndMaxSize);
rulerLayout->setContentsMargins(0, 0, 0, 0);
horizontalLayout->addWidget(rulerFrame);
Re: Can't get geometry() in form constructor
The geometry gets calculated only when the widget gets visible, and in the constructor, it is not yet visible.
One way to over come this, is to initialize your children in the showEvent() handler.
Re: Can't get geometry() in form constructor
Tried this :
Code:
{
createRuler(_lower_bound, _upper_bound, _steps);
}
Tnx for solution.
Re: Can't get geometry() in form constructor
Note, that you code will create the ruler EVERY time your widget gets a show event, and that can happen when you resize, or when it was under another windows that is moved etc.
You might want to add a condition logic to when it should do this - and not for every show event.
Re: Can't get geometry() in form constructor
I've got feeling that you should react on QWidget::resizeEvent and there do the calculations.
Re: Can't get geometry() in form constructor
resizeEvent for the ruler, of course...