Qt QDialog用法总结 笔记(持续更新中)

#include "mainwindow.h"
#include
#include
#include
#include
#include
#include
#include
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QMenuBar *mBar=menuBar();
   QMenu *menu=mBar->addMenu("对话框");
   QAction *modalDialog=menu->addAction("模态对话框");
   QAction *modelessDialog=menu->addAction("非模态对话框");
   connect(modalDialog,&QAction::triggered,
           [=]()
            {
                QDialog dlg;
                dlg.exec();
            }
           );
   connect(modelessDialog,&QAction::triggered,
           [=]()
            {

                //dlg.show();
                QDialog *p=new QDialog;
                p->setAttribute(Qt::WA_DeleteOnClose);
                p->show();

            }
           );

   QAction *p1=menu->addAction("关于对话框");
   connect(p1,&QAction::triggered,
           [=]()
            {
                QMessageBox::about(this,"about","关于QT");
            }
           );


   QAction *p2=menu->addAction("问题对话框");
   connect(p2,&QAction::triggered,
           [=]()
            {
               int ret= QMessageBox::question(this,"question","Are you OK?",
                                              QMessageBox::Ok |QMessageBox::Cancel
                                              );
               switch(ret)
               {
                   case QMessageBox::Ok:
                       qDebug()<<"I am OK";
                       break;
                   case QMessageBox::Cancel:
                       qDebug()<<"I am bad";
                                 break;
                   default:
                       break;
               }
            }
           );

   QAction *p3=menu->addAction("文件对话框");
   connect(p3,&QAction::triggered,
           [=]()
            {
                QString path=QFileDialog::getOpenFileName(
                            this,
                            "open",
                            "../",
                            "source(*.cpp *.h);;"
                            "Text(*.txt);;"
                            "all(*.*)"
                            );
                qDebug()<

模态对话框也可以这么设置

QDialog *p=new QDialog;
                p->setWindowTitle("表格属性框");
                p->setAttribute(Qt::WA_DeleteOnClose);
                p->setWindowModality(Qt::ApplicationModal);

这样即是模态框,每次创建的指针在关闭后也会自动销毁,而不是等到整个程序关闭才销毁。

你可能感兴趣的:(Qt QDialog用法总结 笔记(持续更新中))