Skip to main content

Command Palette

Search for a command to run...

Example of Nested Layout Version2

Using the concept of 'this' pointer

Updated
2 min read
Example of Nested Layout Version2
S

An Electrical Engineer who is enthusiastic about coding , currently working with Qt and taking up these challenges in twitter to build connstructive habits.

#100DaysOfCode

#100DaysOfWriting

#100DaysOfReading

#100DaysOfMeditation

Introduction

In the previous example, I have commented w.show() in main.cpp to suppress the by-default window, which is not a healthy practice. So, in this example, I have made it possible to show the contents of nestedlayout.cpp in the window generated by main.cpp by using this.

Codes

Have a look at the following code

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.h

#ifndef NESTEDLAYOUT_H
#define NESTEDLAYOUT_H

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

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

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(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);
    QMainWindow::setCentralWidget(primaryWindow);  // Making widget 
                                                                                           primaryWindow 
                                                                                          as the central widget of the main 
                                                                                          window
    primaryWindow->setWindowTitle("Nested Layout");

}

NestedLayout::~NestedLayout()
{
}

Important Points

The changes that I've done in the above code with respect to the code in my previous posts are:

1) In nestedlayout.cpp

    QWidget *primaryWindow = new QWidget(this);

In this line, this pointer points to the main window, parent window of main.cpp .

2) In nestedlayout.cpp

     QMainWindow::setCentralWidget(primaryWindow);

This line helps in setting primaryWindow widget as the central widget of the main window, thereby transferring the contents of primaryWindow to the main window.

The result obtained in this case is same as the previous one without commenting w.show()

Output

NestedLayoutWithRange.PNG

In the next post, I will be presenting the same code with different base classes like QDialog and QWidget and study the changes.

Know Qt

Part 5 of 11

In this series, I will discuss about the pre-requisites to learn Qt. I will also be discussing the confusions cropped up in my mind as a beginner of Qt. I hope it helps you in your journey!

Up next

Example of Nested Layout Version3

With QDialog as base class