当前位置:网站首页>QT modal dialog and non-modal dialog learning

QT modal dialog and non-modal dialog learning

2022-08-10 03:22:00 Progress every day 2015

QTmodal dialogs and Modelessdialog

The concepts of modal and modeless dialogs are not unique to Qt, exists on a variety of platforms.It is also called a modal dialog box, a modeless dialog box, etc.The so-called modal dialog box means that the user cannot interact with other windows of the same application until the dialog box is closed until the dialog box is closed.For modeless dialogs, when opened, the user can choose to interact either with the dialog or with other windows of the application.

In Qt, there are generally two ways to display a dialog box, one is to use the exec() method, which always displays the dialog box in modal; the other is to use the show() method, which makes the dialog both modal and non-modal displayed, deciding that it is modalOr modeless is the modal property of the dialog.

In Qt, Qt's modal and modeless dialog selection is done through its propertiesmodal to determine.Let's take a look at the modal property, which is defined as follows:

modal : bool By default, the property value of the dialog box is false, and the dialog box displayed by the show() method is non-modal.And if the value of this property is set to true, it is set to a modal dialog, which acts to set the QWidget::windowModality property to Qt::ApplicationModal.

If the dialog is displayed using the exec() method, the setting of the modal property value will be ignored and the dialog will be set as a modal dialog.

Generally use the setModal() method to set the modal property of the dialog box.

Let's summarize how to set a dialog box to be modal.

◆ If you want to set it as a modal dialog, the easiest way is to use the exec() method, the sample code is as follows:

MyDialog myDlg; myDlg.exec(); You can also use the show() method, the sample code is as follows:

MyDialog myDlg; myDlg.setModal(true); myDlg.show();

◆ If you want to set it as a modeless dialog, you must use the show() method, the sample code is as follows:

MyDialog myDlg; myDlg.setModal(false);
//or
myDlg.setModal();
myDlg.show();
There is a misunderstanding of the understanding of >modal dialogs and modeless dialogs. It is believed that the show() method is used to display modeless dialogs.is incorrect.

Tips: Sometimes, we need a dialog box to be displayed in a non-modal form, but we need it to always be at the front of all windows. In this case, it can be set by the following code:

MyDialog myDlg; myDlg.setModal(false);
//Or
myDlg.setModal(); myDlg.show();
//The key is the following line
myDlg.setWindowFlags(Qt::WindowStaysOnTopHint);

Create a modal dialog in Qt, mainly using QDialog's 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!”);

}

As shown in the above code, the return value of the exec function can be used to determine which button the user clicked to cause the modal dialog to exit.Takes a different approach after exiting a modal dialog.


Attentive readers may ask, since it is new, if it is not deleted, is there a problem of memory leakage?It does!Therefore, we hope that the Qt window can automatically delete itself when it exits. Therefore, we add this code in the constructor of SonDialog:

setAttribute(Qt::WA_DeleteOnClose);

In this way, our SonDialog can automatically delete itself when it exits, and will no longer cause memory leaks

原网站

版权声明
本文为[Progress every day 2015]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208100201144953.html