模态与非模态

QDialog的显示有两个函数show()exec()

他们的区别在参考文档上的解释如下:

 

1.show():
显示一个非模式对话框。控制权即刻返回给调用函数。
弹出窗口是否模式对话框,取决于modal属性的值。
(原文:Shows the dialog as a modeless dialog. Control returns immediately to the calling code. 
The dialog will be modal or modeless according to the value of the modal property. )

 

2.exec():
显示一个模式对话框,并且锁住程序直到用户关闭该对话框为止。函数返回一个DialogCode结果。
在对话框弹出期间,用户不可以切换同程序下的其它窗口,直到该对话框被关闭。
(原文:Shows the dialog as a modal dialog, blocking until the user closes it. The function returns a DialogCode result. 
Users cannot interact with any other window in the same application until they close the dialog. )

 

那么,以我的理解,模态的对话框就是在它没有被关闭之前,不能再与同一个应用程序的其他窗口进行交互,比如新建项目的时候弹出来的对话框就属于模态的。

而非模态的对话框不阻塞任何窗口。

 

示例代码(非模态):

#include "mainwindow.h"
#include <QApplication>
#include <QDialog>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QDialog *dlg1=new QDialog();
    dlg1->show();

    QDialog *dlg=new QDialog();
    dlg->setModal(true);
    dlg->exec();

    return a.exec();
}

运行截图:

 

 

 

示例代码(模态):

#include "mainwindow.h"
#include <QApplication>
#include <QDialog>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QDialog *dlg=new QDialog();
    dlg->setModal(true);
    dlg->exec();

    QDialog *dlg1=new QDialog();
    dlg1->show();

    return a.exec();
}

运行截图:

 

 

 

 

区别:

可以看到,两个示例程序中只是将模态与非模态的代码顺序调换了一下,就产生了不同的结果。

 

那么,结果就是在非模态程序中,可以允许两个窗口交替的交互;而在模态的程序中就不行,也即:要先关闭了模态的窗口后才可以弹出非模态的窗口。

 

 

 

TZ

2017.4.7下午于华中农业大学逸夫楼

转载于:https://www.cnblogs.com/acm-icpcer/p/6678268.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值