PDA

View Full Version : Can't get geometry() in form constructor



kartun
28th January 2011, 09:35
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

ui->setupUi(this);
...
createRuler(minValue, maxValue, steps);
in createRuler I've tried this :

int frame_height = ui->rulerLayout->geometry().height() / steps;
But it allways return 0; However inside setupUi there is a code :

gridLayoutWidget = new QWidget(rulerFrame);
gridLayoutWidget->setObjectName(QString::fromUtf8("gridLayoutWidget"));
gridLayoutWidget->setGeometry(QRect(0, 0, 61, 361));
rulerLayout = new QGridLayout(gridLayoutWidget);
rulerLayout->setSpacing(6);
rulerLayout->setMargin(11);
rulerLayout->setObjectName(QString::fromUtf8("rulerLayout"));
rulerLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
rulerLayout->setContentsMargins(0, 0, 0, 0);

horizontalLayout->addWidget(rulerFrame);

high_flyer
28th January 2011, 10:10
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.

kartun
28th January 2011, 10:46
Tried this :

void Gauge::showEvent(QShowEvent *e)
{
QWidget::showEvent( e );
createRuler(_lower_bound, _upper_bound, _steps);
}

Tnx for solution.

high_flyer
28th January 2011, 10:51
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.

MarekR22
28th January 2011, 12:13
I've got feeling that you should react on QWidget::resizeEvent and there do the calculations.

wysota
29th January 2011, 14:03
resizeEvent for the ruler, of course...