Qt – widget with two(or more) layout

layoutqtwidget

I need the way to setup widgets inside other widget with different layouts…

it is something like we have widget divided by one layout into two parts with labels, and
this widget have other widget inside with layout like on attached image alt text

and we have only 4 widgets: main widget, label one widget, label two widget, button widget, and for button use one vertical and two horizontal stretch

Can some body point me to right way do it? Thanks.

Best Answer

Create QVBoxLayout, then add two QHBoxLayouts to it. In top QHBoxLayout add labels, in bottom one add stretch, button, stretch.

window example

#include <QString>
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLocale>

int main(int argc, char** argv){
    QApplication app(argc, argv);

    QWidget widget;

    QVBoxLayout* vLayout = new QVBoxLayout(&widget);
    QHBoxLayout* topLayout = new QHBoxLayout();
    QHBoxLayout* bottomLayout = new QHBoxLayout();
    QLabel* label1 = new QLabel(QObject::tr("Label1"));
    QLabel* label2 = new QLabel(QObject::tr("Label2"));
    label1->setAlignment(Qt::AlignCenter);
    label2->setAlignment(Qt::AlignCenter);
    QPushButton* btn1 = new QPushButton(QObject::tr("The Button!!!!"));
    topLayout->addWidget(label1);
    topLayout->addWidget(label2);
    bottomLayout->addStretch();
    bottomLayout->addWidget(btn1);
    bottomLayout->addStretch();
    vLayout->addLayout(topLayout);
    vLayout->addLayout(bottomLayout);

    widget.show();

    return app.exec();
}