当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
storage of data in memory
QT中,QTableWidget 使用示例详细说明
ECCV 2022 Oral | CCPL: 一种通用的关联性保留损失函数实现通用风格迁移
自动化测试中,测试数据与脚本分离以及参数化方法
openpose脚部标注问题梳理
OpenCV图像处理学习四,像素的读写操作和图像反差函数操作
2022.8.8 Exam written in memory (memory)
What is a Cross-Site Request Forgery (CSRF) attack?How to defend?
2022.8.8 Exam questions for photographer Lao Ma (photographer)
Shell编程--awk
【每日一题】1413. 逐步求和得到正数的最小值
手把手教你搭建ELK-新手必看-第一章:什么是ELK?
【二叉树-中等】508. 出现次数最多的子树元素和
2022.8.8考试区域链接(district)题解
将信号与不同开始时间对齐
2022.8.8考试摄像师老马(photographer)题解
《GB39707-2020》PDF download
官宣出自己的博客了
LeetCode每日两题02:两数之和 II - 输入有序数组 (均1200道)
2022 Top Net Cup Quals Reverse Partial writeup









