8、模态对话框、非模态对话框

模态对话框:其他对话框无法操作,除非此对话框关闭。非模态对话框,无限制。前者用exec()显示,后者show()显示

新建,基类选择QMainWindow,ui取消勾选

  

#include "mainwindow.h"
//头文件中引入
//#include<QMenuBar>
//#include<QMenu>
//#include<QDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    resize(400,200);
    QMenuBar *mBar=menuBar();
    QMenu *menu=mBar->addMenu("对话框");
    QAction *p1=menu->addAction("模态对话框");
    QAction *p2=menu->addAction("非模态对话框");
    connect(p1,&QAction::triggered,
            [](){
        QDialog *dlg=new QDialog();
        dlg->setWindowTitle("模态对话框");
        dlg->exec();//显示,并等待用户操作。

    });
    connect(p2,&QAction::triggered,
            [](){
        QDialog *dlg=new QDialog();
        dlg->setWindowTitle("非模态对话框");
        dlg->show();
    });
}

MainWindow::~MainWindow()
{

}

猜你喜欢

转载自www.cnblogs.com/xixixing/p/10909294.html