当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
LeetCode每日两题01:移动零 (均1200道)方法:双指针
Data Governance (5): Metadata Management
数组(一)
Arcgis进阶篇(1)——安装Arcgis Enterprise,创建sde库
常用正则备查
Fusion Compute网络虚拟化
2022.8.8 Exam questions for photographer Lao Ma (photographer)
c# 解决CS8602告警 解引用可能出现空引用
微生物是如何影响身体健康的
数据在内存中的存储
牛客刷题——剑指offer(第四期)
基于误差状态的卡尔曼滤波ESKF
Shell编程--awk
Algorithm and voice dialogue direction interview question bank
Pagoda server PHP+mysql web page URL jump problem
论旅行之收获
QT中,QTableWidget 使用示例详细说明
控制台中查看莫格命令的详细信息
MySQL:日志系统介绍 | 错误日志 | 查询日志 | 二进制日志:bin-log数据恢复实践 | 慢日志查询
量化交易策略介绍及应用市值中性化选股