Qt -- 19 modal and modeless dialog boxes

Posted by satya61229 on Thu, 03 Feb 2022 20:42:10 +0100

What are modal and modeless dialog boxes



I can't click other functions and interfaces. This kind of dialog box is called modal dialog box. There are some windows that can operate other windows, which belong to modeless dialog boxes.

Next, create a new project. After the project is created, build the form as shown in the following figure in the design interface (1. Add the menu of file and edit in the menu item,
2. Add a toolbar. 3. Add menu item new in the file menu and put the new item in the toolbar.)

Now, click the new button and there is no response. Therefore, we need to add functions to it. After clicking the new button, a dialog box will pop up. This requires the use of signal and slot mechanism.

Implement modal dialog

In the code to achieve it.

To create a dialog box, you need to add a header file #include < qdialog >

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDialog>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //Click the new button to pop up a dialog box
    connect(ui->actionNew,&QAction::triggered,[=](){
        //There are two categories of dialog boxes: modal (you can't operate on other windows) and non modal (you can operate on other windows)
        //Modal creation
        QDialog dlg(this);//Specify the father to avoid manual release
        dlg.exec();//With blocking function, it is impossible to operate other windows

        qDebug() << "The modal dialog box pops up";
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

Run the code and click the new button. The dialog box will pop up, as shown in the following figure.

When the dialog box appears, you can't click other windows. Moreover, the debugging information is not printed out in the application output window. This is because the dialog box we created is a modal dialog box. When the program runs, it will be blocked in the

  dlg.exec();//With blocking function, it is impossible to operate other windows

When the dialog box is closed, the code will be executed next, and the application output window will start to output debugging information.

In addition, you can specify the size of the dialog box created.

 //Click the new button to pop up a dialog box
connect(ui->actionNew,&QAction::triggered,[=](){
    //There are two categories of dialog boxes: modal (you can't operate on other windows) and non modal (you can operate on other windows)
    //Modal creation
    QDialog dlg(this);//Specify the father to avoid manual release
    dlg.resize(200,200);
    dlg.exec();//With blocking function, it is impossible to operate other windows

    qDebug() << "The modal dialog box pops up";
});

Implement modeless dialog boxes

The code is as follows:

//Click the open button to pop up a non modal dialog box
connect(ui->actionOpen,&QAction::triggered,[=](){

    QDialog dlg2(this);
    dlg2.resize(200,200);

    dlg2.show();
    qDebug() << "The modeless dialog box pops up";
});

Run the program, you will find that the form flashes by.

Because after executing dlg2 show(); After this code, the dialog box disappears. The essential reason is that this object is placed on the stack. We need to put it on the heap.

The code is as follows:

//Click the open button to pop up a non modal dialog box
connect(ui->actionOpen,&QAction::triggered,[=](){

    QDialog * dlg2 = new QDialog(this);
    dlg2->resize(200,200);

    dlg2->show();
    qDebug() << "The modeless dialog box pops up";
});

The modeless dialog box can operate on other windows.

Although a modeless dialog box can be realized by using the above code, there are the following problems: first, each click will re create a dialog box on the heap.

Second, closing the dialog box will not release the memory on the heap. Only by closing the (total) form will these objects on the heap be released. If you keep clicking, you will always consume memory resources.

So how to solve it?

You can solve this problem by setting the properties of this object. You need to use the set attribute function setAttribute to view the help document.

Which of the following setAttribute functions should I look at? We write a dialog box, so we should check the under the QDialog class, but it is not in the help document. We should see who its parent class is,

// An highlighted block
var foo = 'bar';

It can be seen that QDialog inherits from QWidget, so we should see the setAttribute of QWidget.

There are many attributes of the form. Click QT:: widgettattribute attribute, and take a look at this item (generally, such green ones represent enumeration values),

There are many enumeration values, which also represent many attributes.

Let's talk directly about which attribute should be set. The attribute with the value of 55 should be used.


When the dialog box is closed, the properties can be released on the heap.

The code is as follows:

// An highlighted block
var foo = 'bar';

Topics: Qt UI