当前位置:网站首页>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
边栏推荐
- 【8.8】代码源 - 【不降子数组游戏】【最长上升子序列计数(Bonus)】【子串(数据加强版)】
- 【二叉树-中等】1379. 找出克隆二叉树中的相同节点
- Data Governance (5): Metadata Management
- 2022.8.8考试清洁工老马(sweeper)题解
- 论旅行之收获
- 免费文档翻译软件电脑版软件
- 2022.8.8 Exam written in memory (memory)
- 2022.8.9 Exam Cube Sum--1100 Question Solutions
- Algorithm and voice dialogue direction interview question bank
- Redis - Basic operations and usage scenarios of String|Hash|List|Set|Zset data types
猜你喜欢
随机推荐
2022强网杯 Quals Reverse 部分writeup
【8.8】代码源 - 【不降子数组游戏】【最长上升子序列计数(Bonus)】【子串(数据加强版)】
官宣出自己的博客了
Write a drop-down refresh component
sqlmap dolog外带数据
微生物是如何影响身体健康的
中英文互译在线翻译-在线翻译软件
Pagoda server PHP+mysql web page URL jump problem
翻译工具-翻译工具下载批量自动一键翻译免费
实操|风控模型中常用的这三种预测方法与多分类场景的实现
【二叉树-困难】124. 二叉树中的最大路径和
2022.8.8 exam sweeps the horse (sweeper) antithesis
【Kali安全渗透测试实践教程】第8章 Web渗透
跨站请求伪造(CSRF)攻击是什么?如何防御?
Nacos源码分析专题(五)-Nacos小结
宝塔服务器PHP+mysql网页URL跳转问题
状态压缩小经验
2022 Top Net Cup Quals Reverse Partial writeup
QT中,QTableWidget 使用示例详细说明
Go语言JSON文件的读写操作