------------ ------ | | | 2 | | | | | | 1 | ------ | | ------ | | | 3 | ------------ ------
How to arrange the QGridLayout
like above?
I tried:
QGridLayout *layout = new QGridLayout(); centralWidget->setLayout (layout); layout->addWidget (objOne, 0, 0); layout->addWidget (objTwo, 0, 1); layout->addWidget (objThree, 1, 1);
but failed.
Grid Layout provides a way of dynamically arranging items in a grid. If the GridLayout is resized, all items in the layout will be rearranged. It is similar to the widget-based QGridLayout. All children of the GridLayout element will belong to the layout.
The normal way to add a layout is by calling addLayout() on the parent layout. Once you have added your layout you can start putting widgets and other layouts into the cells of your grid layout using addWidget(), addItem(), and addLayout().
Check the addWidget
documentation. You can provide the rowSpan
and columnSpan
QGridLayout *layout = new QGridLayout(); centralWidget->setLayout (layout); layout->addWidget (objOne, 0, 0, -1, 1); layout->addWidget (objTwo, 0, 1, 1, 1); layout->addWidget (objThree, 1, 1, 1, 1);
Notice however that it is much easier to create the desired layout using QtDesigner. Check this Qt
documentation page for more details
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With