Solved Child Layouts not getting added Properly
-
Following on from @J-Hilk's doubtless-correct analysis:
QVBoxLayout *parentLayout = new QVBoxLayout(this);
If you ran this from a debugger, did you not get a warning message saying "warning adding a layout to ... which already has an existing layout"?
QVBoxLayout *parentLayout = new QVBoxLayout(ui->centralWidget);
Is this the same as
QVBoxLayout *parentLayout = new QVBoxLayout(); ui->centralWidget->setLayout(parentLayout);
?
-
@JonB said in Child Layouts not getting added Properly:
QVBoxLayout *parentLayout = new QVBoxLayout(ui->centralWidget);
Is this the same as
QVBoxLayout *parentLayout = new QVBoxLayout();
ui->centralWidget.seLayout(parentLayout);?
yes, with the setLayout call, the Layout gets reparented to centralWidget.
-
Guys , here my MainWindow is not a QMainWindow,instead ,it inherits from QWidget.Moreover I dont use the .ui form and I create the UI dynamically.
-
@Adit
what's the ui-pointer for than?MainWindow::MainWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWindow)
{ -
Sorry @J-Hilk ,but Im not using it .I tried to add both setMinimumHeight and setMaximumHeight and it works now.Could you tell me when these two functions should be used?
-
@Adit What about http://doc.qt.io/qt-5/qwidget.html#setLayout ?
You created a layout (parentLayout), but you did not tell your MainWindow widget to use it. -
But I have set the MainWindow widget as parent to the Top-Layout created and it works!
-
@Adit
It gets a bit tricky when you add Layouts to Layouts.it should also work, if you specify strechfactors for the addes layouts e.g
parentLayout->setStretch(0,1); parentLayout->setStretch(1,1); parentLayout->setStretch(2,1);
-
I guess stretch factors can be 0 or 1 (contract and expand) right?What if we have multiple Layouts of specific sizes ?
-
@Adit said in Child Layouts not getting added Properly:
I guess stretch factors can be 0 or 1 (contract and expand) right?What if we have multiple Layouts of specific sizes ?
no, 0 means, take as much space, as possible, defined by the sizepolicy and sizehint of the Widget/Layout
in this case a stretch factor of 1 for each element results in the following space for the layouts, 1/3, 1/3 and 1/3.
With for example
parentLayout->setStretch(0,1); parentLayout->setStretch(1,1); parentLayout->setStretch(2,2);
the layouts take up 1/4, 1/4, 1/2 of the available space.
-
If we are using this setStretch() method, then can we use setMaximumWidth() and setMaximumHeight() methods?
-
@Adit yes, the stretch is applied until min and max sizes are hit and as long as no contradicting QSizePolizies are set (like fixed size)
-
Could you suggest me some idea to build a complex layout(i.e sublayouts of varying sizes)?Could you suggest me an opensource project for building a resolution independent UI.