MainWindow Vs Widget's window

MainWindow Vs Widget's window

Introduction

In this blog post, I am going to solve a common confusion that most of you might be having while working on Qt. The confusion is about displaying the widget in the main window or on its own window.

When you create a new file in QtCreator, this is how main.cpp looks like

#include "myApp.h"

#include <QApplication>

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

This shows the mainwindow.

When you start writing code in mainwindow.cpp , you may get confused with two windows which may pop up once you run the code. One is associated with main.cpp and another with mainwindow.cpp. Suppressing the mainwindow by commenting w.show() is not at all a good idea.

So, I will be presenting two examples below to differentiate between the aforementioned scenarios.

Example -1

This is the most common case when both windows appear on the screen when the program is run.

myApp.h

#ifndef MYAPP_H
#define MYAPP_H

#include <QMainWindow>


class myApp : public QMainWindow
{
    Q_OBJECT

public:
    myApp(QWidget *parent = nullptr);

};
#endif // MYAPP_H

main.cpp

#include "myApp.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    myApp w;
    w.setWindowTitle("Example of mainWindow Vs Widget's Window");
    w.show();
    return a.exec();
}

myApp.cpp

#include "myApp.h"
#include <QLineEdit>

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

    QLineEdit *modelLabel = new QLineEdit(parent);
    //modelLabel->setFixedSize(100,100); // Through this you can resize the widget to a fixed size in the dimension of width and height.
    modelLabel->setObjectName("readOnly");
    modelLabel->setWindowTitle("myApp's window");
    modelLabel->show();

}

image.png

As you can see in the above figure, there are two windows corresponding to main.cpp and myApp.cpp.

Example -2

When you don't want two separate windows when the program is run, it's better to make the widget the central widget of the main window. It's a convenient way to merge the widget with the main window and you no longer have to deal with the widget's window.

As there's no separate window for the widget's window,w.show() of the main window will be enough to display the contents of the mainwindow which now contains the widget.

myApp.h

#ifndef MYAPP_H
#define MYAPP_H

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

public:
    myApp(QWidget *parent = nullptr);
};
#endif // MYAPP_H

main.cpp

#include "myApp.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    myApp w;
    w.setWindowTitle("Example of mainWindow Vs Widget's Window");
    w.show();
    return a.exec();
}

myApp.cpp

#include "myApp.h"
#include <QLineEdit>

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

    QLineEdit *modelLabel = new QLineEdit(parent);
    this->setCentralWidget(modelLabel);
}

image.png

I hope that this clarifies the confusions related to windows associated with main.cpp and mainwindow.cpp.

Did you find this article valuable?

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