Example of Nested Layout

Example of Nested Layout

Horizontal Layout nested within Vertical Layout

Introduction

This is a simple app of Student Database for storing counts of students classwise and genderwise. I have only taken two classes for simplicity, to emphasize the concept of layout management and nesting of layouts. However, this code can be extended to form a generic code for storing information of different classes which can be achieved through QMap or QVector. Since these are little advanced topics,I've kept them for discussion for later.

Codes

main.cpp

#include "nestedlayout.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    NestedLayout w;
    //w.show();
    return a.exec();
}

NOTE : w.show() should not be commented. In my next post, I will state its reason.

Case -1 [When the range for total students of the school is not set; by default the range is 0 to 99 for a spinbox Widget]

nestedlayout.cpp

#include "nestedlayout.h"
#include <QWidget>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSpinBox>

NestedLayout::NestedLayout(QWidget *parent)
    : QMainWindow(parent)
{

    QWidget *primaryWindow = new QWidget();

    QVBoxLayout *primaryLayout = new QVBoxLayout();

    QLabel *mainHeading = new QLabel("Students DataBase");

    QLabel *class1 = new QLabel("Total Students in Class-1"); //Class -1
    QSpinBox *class1Count = new QSpinBox();


    //Details of Class-1
    QHBoxLayout *class1Title = new QHBoxLayout();
    QLabel *class1Boys = new QLabel("Number of Boys");
    QLabel *class1Girls = new QLabel("Number of Girls");
    class1Title->addWidget(class1Boys);
    class1Title->addWidget(class1Girls);

    QHBoxLayout *class1Values = new QHBoxLayout();
    QSpinBox *class1BoysCount = new QSpinBox();
    QSpinBox *class1GirlsCount = new QSpinBox();
    class1Values->addWidget(class1BoysCount);
    class1Values->addWidget(class1GirlsCount);

    QLabel *class2 = new QLabel("Total Students in Class-2"); //Class: 2
    QSpinBox *class2Count = new QSpinBox();


    //Details of class-2
    QHBoxLayout *class2Title = new QHBoxLayout();
    QLabel *class2Boys = new QLabel("Number of Boys");
    QLabel *class2Girls = new QLabel("Number of Girls");
    class2Title->addWidget(class2Boys);
    class2Title->addWidget(class2Girls);

    QHBoxLayout *class2Values = new QHBoxLayout();
    QSpinBox *class2BoysCount = new QSpinBox();
    QSpinBox *class2GirlsCount = new QSpinBox();
    class2Values->addWidget(class2BoysCount);
    class2Values->addWidget(class2GirlsCount);

    QLabel *class22 = new QLabel("Total Students in school"); //Total Srength of 
                                                                                                        students in School  
    QSpinBox *class2Count2 = new QSpinBox();

    //Adding all widgets and layouts to the main layout ;primaryLayout

    primaryLayout->addWidget(mainHeading);
    primaryLayout->addWidget(class1);
    primaryLayout->addWidget(class1Count);
    primaryLayout->addLayout(class1Title);
    primaryLayout->addLayout(class1Values);
    primaryWindow->setLayout(primaryLayout);
    primaryLayout->addWidget(class2);
    primaryLayout->addWidget(class2Count);
    primaryLayout->addLayout(class2Title);
    primaryLayout->addLayout(class2Values);
    primaryLayout->addWidget(class22);
    primaryLayout->addWidget(class2Count2);
    primaryWindow->setWindowTitle("Nested Layout");
    primaryWindow->show();

}

NestedLayout::~NestedLayout()
{
}

Output-1

NestedLayout.PNG

Case -2 [When the range of total students of the school is decided]


#include "nestedlayout.h"
#include <QWidget>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSpinBox>

NestedLayout::NestedLayout(QWidget *parent)
    : QMainWindow(parent)
{

    QWidget *primaryWindow = new QWidget();

    QVBoxLayout *primaryLayout = new QVBoxLayout();

    QLabel *mainHeading = new QLabel("Students DataBase");

    QLabel *class1 = new QLabel("Total Students in Class-1"); //Class -1
    QSpinBox *class1Count = new QSpinBox();


    //Details of Class-1
    QHBoxLayout *class1Title = new QHBoxLayout();
    QLabel *class1Boys = new QLabel("Number of Boys");
    QLabel *class1Girls = new QLabel("Number of Girls");
    class1Title->addWidget(class1Boys);
    class1Title->addWidget(class1Girls);

    QHBoxLayout *class1Values = new QHBoxLayout();
    QSpinBox *class1BoysCount = new QSpinBox();
    QSpinBox *class1GirlsCount = new QSpinBox();
    class1Values->addWidget(class1BoysCount);
    class1Values->addWidget(class1GirlsCount);

    QLabel *class2 = new QLabel("Total Students in Class-2"); //Class: 2
    QSpinBox *class2Count = new QSpinBox();


    //Details of class-2
    QHBoxLayout *class2Title = new QHBoxLayout();
    QLabel *class2Boys = new QLabel("Number of Boys");
    QLabel *class2Girls = new QLabel("Number of Girls");
    class2Title->addWidget(class2Boys);
    class2Title->addWidget(class2Girls);

    QHBoxLayout *class2Values = new QHBoxLayout();
    QSpinBox *class2BoysCount = new QSpinBox();
    QSpinBox *class2GirlsCount = new QSpinBox();
    class2Values->addWidget(class2BoysCount);
    class2Values->addWidget(class2GirlsCount);

    QLabel *class22 = new QLabel("Total Students in school"); //Total Srength of 
                                                                                                       students in School
    QSpinBox *class2Count2 = new QSpinBox();
    class2Count2->setRange(0,1000); //With Range


    primaryLayout->addWidget(mainHeading);
    primaryLayout->addWidget(class1);
    primaryLayout->addWidget(class1Count);
    primaryLayout->addLayout(class1Title);
    primaryLayout->addLayout(class1Values);
    primaryWindow->setLayout(primaryLayout);
    primaryLayout->addWidget(class2);
    primaryLayout->addWidget(class2Count);
    primaryLayout->addLayout(class2Title);
    primaryLayout->addLayout(class2Values);
    primaryLayout->addWidget(class22);
    primaryLayout->addWidget(class2Count2);
    primaryWindow->setWindowTitle("Nested Layout");
    primaryWindow->show();

}

NestedLayout::~NestedLayout()
{
}

Output-2

NestedLayoutWithRange.PNG

Instead of adding the contents of class-1 like titles and values as different horizontal layouts, the entire information of classes can be enclosed within a widget which can be added to the main layout. I will show its implementation in my subsequent post for dealing with complex data types.

Did you find this article valuable?

Support Swati Sarangi by becoming a sponsor. Any amount is appreciated!