QT modal dialog and modeless dialogs

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

In Qt to create the modal dialog box, the main use of the QDialog exec function:
SonDialog dlg(this);
int res = dlg.exec();
if (res == QDialog::Accepted)
{
QMessageBox::information(this, “INFORMATION”, “You clicked OK button!”);
}
if (res == QDialog::Rejected)
{
QMessageBox::information(this, “INFORMATION”, “You clicked CANCEL button!”);
}
正如上面代码所显示的,可以通过exec函数的返回值来判断用户点击了哪个按钮使得 模态对话框退出的,这可以使得我们能够根据用户的不同行为在退出 模态对话框之后采取不同的处理方法。

Guess you like

Origin blog.csdn.net/weixin_42536869/article/details/80795946