Modal dialog (Modal Dialog) and the non-modal dialog box (Modeless Dialog) concept is not Qt unique, in the presence of a variety of different platforms. There it is called is called a modal dialog box, such as non-modal dialog box. The so-called modal dialog box is not closed before it, the user can not interact with the same application of other windows until the dialog box is closed. For non-modal dialog box, when opened, the user can select and interact with the dialog box, select other windows can also interact with the application.
In Qt , the display a dialog box there are two ways, one is to use exec () method, which always modality to display the dialog box; the other is to use the show () method, which allows dialog either mode can also be displayed modeless display, it is decided modal or modeless the modal properties dialog box.
In Qt in, Qt the modal and modeless dialog box to select is determined by its properties modal. We take a look modal properties, which are defined as follows:
modal: bool default case, the attribute value dialog is false, so the show is displayed in the dialog method () modeless of. If the attribute value is set to true, it became modal dialog provided, which acts on the QWidget :: windowModality property to the Qt :: ApplicationModal.
Used exec () method then displays a dialog box, modal attribute value is ignored and the dialog box to set the modal dialog box.
Generally used setModal () method to set the properties modal dialog.
We summarize the Settings dialog box is modal approach.
◆ To set modal dialog box, the simplest is to use the exec () method, the following sample code:
MyDialog myDlg; myDlg.exec (); may be used show () method, the following sample code:
MyDialog myDlg; myDlg.setModal(true); myDlg.show();
◆ To set modeless dialog must show () method, the following sample code:
MyDlg MyDialog; myDlg.setModal (false);
// or
myDlg.setModal ();
myDlg.show ();
Again, currently some friends for a modal dialog box and the non-modal understanding of the dialog box there is the misconception that the use of show () method is displayed modeless dialog boxes, which is not correct.
Tips: Sometimes we need a dialog to modeless display form, but it is always on top of all requires a window, then can be provided by the following code:
MyDlg MyDialog; myDlg.setModal (false);
// or
myDlg.setModal (); myDlg.show ();
// The key is this line
myDlg.setWindowFlags ( Qt :: WindowStaysOnTopHint);