Many applications, like a photoshop, can open projects (images, texts, etc.) inside windows that open inside the main application window. Qt provides a similar function as QMdiArea . In an object of this class, you can place class objects inherited from the QWidget class, and accordingly the QWidget class. These objects will display as windows, only they will be located inside QMdiArea .
Let's see an example with a window inside the window.
Project structure
The following files are included in the project:
- MdiWindow.pro - project profile, created by default;
- main.cpp - the startup file of the program with the main function, created by default;
- mainwindow.h - header file of the main application window;
- mainwindow.cpp - the source code file for the main application window;
- mainwindow.ui - form of the main application window;
- icons.qrc - resource file, contains one icon.
Files MdiWindow.pro and main.cpp will not be considered, since they are created by default. The file icons.qrc contains one icon, which is visible on the toolbar of the main application window. This button is responsible for creating an additional application window and is added through the graphic designer. This Action is only needed to emit a signal to add new windows to QMdiArea .
Now let's move on to the essence of the project.
mainwindow.h
Header file of the main application window. Contains the declaration of the pointer to the QMdiArea object, as well as the declaration of the slot for adding new windows, which was created through the graphic designer.
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QMdiArea> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_actionAddWindow_triggered(); private: Ui::MainWindow *ui; QMdiArea * mdiArea; // Area in which windows will be added }; #endif // MAINWINDOW_H
mainwindow.cpp
The QMdiArea object is installed as the central widget for the main application window. Also, configure the scrollbars so that they appear only when necessary. In the add window add a QWidget object with an inscription. There is one point. If QWidget does not contain any content, it will open with a minimum size, even if you size it. And only when you try to move the window, this widget will take the size displayed.
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QGridLayout> #include <QLabel> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); mdiArea = new QMdiArea(this); // init QMdiArea // configure scrollbars mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); // Set Mdi Area as the central widget setCentralWidget(mdiArea); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_actionAddWindow_triggered() { // Create a widget that will be a window QWidget *widget = new QWidget(mdiArea); // Adding layout to it QGridLayout *gridLayout = new QGridLayout(widget); widget->setLayout(gridLayout); // Adding an label to the widget QLabel *label = new QLabel("Hello, I am sub window!!!", widget); gridLayout->addWidget(label); // Adding a widget as a sub window in the Mdi Area mdiArea->addSubWindow(widget); // Set the window title widget->setWindowTitle("Sub Window"); // And show the widget widget->show(); }