zoukankan      html  css  js  c++  java
  • 模态与非模态

    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下午于华中农业大学逸夫楼

  • 相关阅读:
    解决前端跨域请求的几种方式
    使用excel 展现数据库内容
    win7 安装windows 服务 报错 System.Security.SecurityException 解决方法 An exception occurred during the Install phase. System.Security.SecurityException: The so
    java 查看线程死锁
    linux 配置 java 环境变量
    数据库性能优化
    C#中静态与非静态方法比较
    apache日志切割工具cronolog安装配置
    虚拟机克隆后网卡有问题解决方法
    vs2015工具箱突然没有了
  • 原文地址:https://www.cnblogs.com/acm-icpcer/p/6678268.html
Copyright © 2011-2022 走看看