PDA

View Full Version : Why doesnt my QGridLayout resize?



tgreaves
19th January 2009, 21:55
This resizes perfectly ------

QGridLayout *layout = new QGridLayout(this);
layout->setSpacing(6);
layout->setMargin(11);
QPlainTextEdit* mybox = new QPlainTextEdit(this);
layout->addWidget(mybox, 0, 0, 1, 2);
setLayout(layout);

This doesnt resize at all ------

QWidget* widget = new QWidget(this)
QGridLayout *layout = new QGridLayout(widget);
layout->setSpacing(6);
layout->setMargin(11);
QPlainTextEdit* mybox = new QPlainTextEdit(this);
layout->addWidget(mybox, 0, 0, 1, 2);
setLayout(layout);

The only difference is with the 2nd example, im creating a widget* and making the layout a child of that widget which is what Qt Designer does.. How can I make this resize work using the 2nd example?

codeslicer
19th January 2009, 22:16
Try:



QGridLayout *layout = new QGridLayout(widget);
QWidget* widget = new QWidget(this);
layout->setSpacing(6);
layout->setMargin(11);
QPlainTextEdit* mybox = new QPlainTextEdit(this);
layout->addWidget(mybox, 0, 0, 1, 2);
layout->addWidget(widget, 0, 0, 1, 3); //<--or whatever you need
setLayout(layout);

jpn
20th January 2009, 07:19
QLayout constructor already calls QWidget::setLayout(). So the following code doesn't make sense:

QGridLayout *layout = new QGridLayout(widget);
...
setLayout(layout);
First you install the layout on "widget", then you attempt to install the same layout on "this".