“This is the 25th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”
Modal and modeless dialogs
The QDialog class is the base class for all dialog window classes. The dialog window is a top-level window that is often used to complete short tasks or simply interact with the user. Dialogs are often divided into modal dialogs and modeless dialogs according to whether the dialogs can also interact with other Windows of the program while running.
- Modal dialogs can no longer interact with other Windows in the same application until the dialogs are closed.
- Modeless dialogs can interact with Windows in consent programs.
Create dialog box method:
There are three ways to create dialogs:
1. Create a modeless dialog box using new:
QDialog *dialog = new QDialog(this);
dialog->show();
Copy the code
Specify the parent window of the window by new a QDialog object pointer and calling show() to display it. This will work, except that the window is modal, and it will not prevent the code from working properly after this code. And because the parent window is specified, there is no need to use delete() to free the object; all child Windows are automatically freed when the parent window is destroyed.
2. Define temporary variables to create a modal dialog:
QDialog dialog(this);
dialog.exec();
Copy the code
Start a modal dialog box in this way. When the program reaches this point, a dialog box pops up, and any code following this code will not execute until the dialog box is closed.
3. Create a modal dialog box using new:
QDialog *dialog = new QDialog(this);
dialog->setModal(true);
dialog->show();
Copy the code
Dialogs launched in this way are modal, and differ from the first method by calling the setModal() function. Although this is a modal dialog box, but the code after this code can be normally executed. This is because the show() call immediately hands control to the caller and the program can continue. When the exec() function is called, it returns only when the dialog box is closed.
SetModal () function:
Similar to the setModal() function, there is the setWindowModality() function, which takes an argument to set the type of window to block in the modal dialog box. This argument can be:
- Qt::NonModal()// If it does not block any Windows, it is modal
- Qt::WindowModal()// blocks its parent window, all ancestor Windows, and their child Windows
- Qt::ApplicationModal()// blocks all Windows throughout the application
The setModal() function defaults to Qt::ApplicationModal.