Example of Nested Layout Version3

Example of Nested Layout Version3

With QDialog as base class

Table of contents

Introduction

I often get confused with choosing between QMainWindow, QWidget and QDialog as the base class. In this example, I mean to say that choosing a base class (out of the above three) for NestedLayout class has been a confusing task for me. Actually, the base class should be chosen based on the usage and application. So, let me explain it a bit further.

QWidget is the parent class from which QMainWindow class and QDialog class have been derived. Having said that, it holds invariably true that QMainWindow and QDialog classes will definitely be having functionalities of QWidget class plus some of its own features.

QMainWindow provides additional features such as menu bar, toolbar, dock widgets, central widgets etc. whereas QDialog provides a temporary pop-up window for short-term tasks involving brief -interaction with the user. It's always a top-level widget but in case it has a parent, it is centered on the top of the top-level widget of the parent.

Refer to https://codingstars.hashnode.dev/example-of-nested-layout-version2 to check its implementation using base class as QMainWindow.

Codes

With base class as QDialog

NestedLayout.h

#ifndef NESTEDLAYOUT_H
#define NESTEDLAYOUT_H

#include <QMainWindow>
#include <QDialog>
class NestedLayout : public QDialog
{
    Q_OBJECT

public:
    NestedLayout(QWidget *parent = nullptr);
};
#endif // NESTEDLAYOUT_H

main.cpp

#include "nestedlayout.h"

#include <QApplication>

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

nestedlayout.cpp


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

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

    QWidget *primaryWindow = new QWidget(this);

    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");

}

Output

NestedLayoutQDialog_.PNG

For more information, you can always refer to the official Qt Documentation. Through this program, I've tried to simplify the confusion which I've come across as a beginner of Qt. I hope that you are able to understand the difference between selecting different base classes.

In my next post, I will discuss about another way to achieve this design by enclosing the whole program into smaller widgets.

Did you find this article valuable?

Support Coding Concoction by becoming a sponsor. Any amount is appreciated!