Evgenii Legotckoi
Evgenii LegotckoiApril 25, 2017, 12:23 p.m.

Qt/C++ - Tutorial 063. Adding windows inside the main application window using QMdiArea

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();
}

Download the project with an example QMdiArea

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
AD

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:50points,
  • Rating points-4
m

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:80points,
  • Rating points4
m

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:20points,
  • Rating points-10
Last comments
i
innorwallNov. 13, 2024, 11:03 p.m.
How to make game using Qt - Lesson 3. Interaction with other objects what is priligy tablets What happens during the LASIK surgery process
i
innorwallNov. 13, 2024, 8:09 p.m.
Using variables declared in CMakeLists.txt inside C ++ files where can i buy priligy online safely Tom Platz How about things like we read about in the magazines like roid rage and does that really
i
innorwallNov. 11, 2024, 10:12 p.m.
Django - Tutorial 055. How to write auto populate field functionality Freckles because of several brand names retin a, atralin buy generic priligy
i
innorwallNov. 11, 2024, 6:23 p.m.
QML - Tutorial 035. Using enumerations in QML without C ++ priligy cvs 24 Together with antibiotics such as amphotericin B 10, griseofulvin 11 and streptomycin 12, chloramphenicol 9 is in the World Health Organisation s List of Essential Medici…
i
innorwallNov. 11, 2024, 3:50 p.m.
Qt/C++ - Lesson 052. Customization Qt Audio player in the style of AIMP It decreases stress, supports hormone balance, and regulates and increases blood flow to the reproductive organs buy priligy online safe Promising data were reported in a PDX model re…
Now discuss on the forum
i
innorwallNov. 13, 2024, 6:52 p.m.
добавить qlineseries в функции PMID 35774217 Free PMC article priligy cvs
i
innorwallNov. 11, 2024, 10:55 a.m.
Всё ещё разбираюсь с кешем. priligy walgreens levitra dulcolax carbs The third ring was found to be made up of ultra relativistic electrons, which are also present in both the outer and inner rings
9
9AnonimOct. 25, 2024, 9:10 a.m.
Машина тьюринга // Начальное состояние 0 0, ,<,1 // Переход в состояние 1 при пустом символе 0,0,>,0 // Остаемся в состоянии 0, двигаясь вправо при встрече 0 0,1,>…

Follow us in social networks