tags:

views:

46

answers:

2

How do I get the final height of a widget that was just added to a layout, but because of this, it has not resized to its final size?

Basically I need to do this:

myGridLayout->addWidget ( somewidget, 0, 0, 1, 1 );

QPushButton *b = new QPushButton(somewidget);
b->setGeometry( somewidget->width() - 50,
                somewidget->height()/2 - 150,
                50, 300);

What I want to do is to insert a button (50x300) in the middle of the right edge of the just added widget, but for this I need to know the dimensions of the widget, which I'm not getting correctly because it has not resized to its full size in the layout. Any ideas?

+1  A: 

Normally, the content of a layout is calculated right before being displayed. If you need to force it earlier, you can call either QLayout::activate() or QLayout::update(), which redoes or updates the layout for its parent widget.

Fred
Thanks for your answer. I'll try this.
Daniel
I tried, and none of the worked for me. However, I decided going in another direction. See chosen answer. Thanks for your help!
Daniel
A: 

If I understand you correctly, this should accomplish what you are trying to do without trying to catch the size at the right time. Add a layout to your 'somewidget' and add the button to that.

myGridLayout->addWidget(somewidget, 0, 0, 1, 1 );

QGridLayout *layout = new QGridLayout(somewidget);
QPushButton *button = new QPushButton();
button->setText("button");
button->setMaximumSize(50,300);
button->setMinimumSize(50,300);
button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
layout->addWidget(button, 0, 0, Qt::AlignVCenter | Qt::AlignRight);
Arnold Spence
Thanks for your answer. I thought about this, but there is a problem. You would need to set somewidget's layout to 'layout' by doing: somewidget->setLayout(layout). Won't this cause a mess with what is already within 'somewidget'?
Daniel
Yes, that would complicate it a bit. I would experiment with layouts and probably nested layouts to possibly achieve what you want. Short of that, if somewidget is a subclass of your own, you should re-implement resizeEvent(). This will always give you the proper geometry for that widget.
Arnold Spence
My solution was to create my own widget that has a grid layout, in which I put 'somewidget'. It would be 3 by 3 grid, where the central/middle cell would be occupied by 'somewidget', and on the edge cells I would put the buttons, exactly where I need. It is similiar to your solution. Thanks for your answer!
Daniel