#include "mainwindow2.h" #include #include MainWindow2::MainWindow2(QWidget *parent) : QMainWindow(parent) { setWindowTitle("对话框学习中..."); setWindowIcon(QIcon(":/icon/open")); resize(1200,960); mEdit = new QTextEdit(this); mEdit->close(); setCentralWidget(mEdit); setCentralWidget(new QTextEdit(this)); QStatusBar *status_bar = new QStatusBar(this); btn1 = new QPushButton("按钮1",status_bar); btn2 = new QPushButton("按钮2",status_bar); btn3 = new QPushButton("按钮3",status_bar); btn4 = new QPushButton("定制MessageBox",status_bar); btn5 = new QPushButton("button5",status_bar); status_bar->addWidget(btn1); status_bar->addWidget(btn2); status_bar->addWidget(btn3); status_bar->addWidget(btn4); status_bar->addWidget(btn5); setStatusBar(status_bar); connect(btn1,&QPushButton::clicked,[&](){ QDialog dialog; dialog.setWindowTitle(tr("自定义对话框 %1").arg(1)); // dialog.exec(); // 应用级别的模态对话框 dialog.show(); }); // connect(btn2,&QPushButton::clicked,[&](){ // QDialog *dialog = new QDialog(this); // dialog->setWindowTitle(tr("自定义对话框 %1").arg(2)); // dialog->setAttribute(Qt::WA_DeleteOnClose); // 当关闭对话时,自动释放堆空间 // dialog->open(); // 窗口级别的模态对话框 // }); // connect(btn3,&QPushButton::clicked,[&](){ // QDialog *dialog = new QDialog(this); // dialog->setWindowTitle(tr("自定义对话框 %1").arg(3)); // dialog->setAttribute(Qt::WA_DeleteOnClose); // 当关闭对话时,自动释放堆空间 // dialog->show(); // 窗口级别的非模态对话框 // }); connect(btn2,&QPushButton::clicked,[&](){ QMessageBox::warning(this,"警告","你的操作可能会失败..."); }); connect(btn3,&QPushButton::clicked,[&](){ // QMessageBox::StandardButton ret; // ret = QMessageBox::question(this,"询问","选什么内容?"); // if(ret == QMessageBox::Yes) // qDebug()<<"选了行"; // else // qDebug()<<"没选行"; // QMessageBox::critical(this,"警告","太警告了"); QMessageBox::information(this,"info","no info",QMessageBox::NoButton,QMessageBox::Apply); }); connect(btn4,&QPushButton::clicked,[&](){ QMessageBox *msgBox = new QMessageBox(this); msgBox->setAttribute(Qt::WA_DeleteOnClose); // msgBox->setIcon(QMessageBox::Critical); // msgBox->setWindowTitle("自定义错误提示"); msgBox->setText("Error"); msgBox->setInformativeText("An error occurred while processing the request."); msgBox->setStandardButtons(QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel); msgBox->setDefaultButton(QMessageBox::Cancel); int ret = msgBox->exec(); // 会返回一个 int switch (ret) { case QMessageBox::Ok: qDebug()<<"Ok"; break; case QMessageBox::Yes: qDebug()<<"Yes"; break; case QMessageBox::No: qDebug()<<"No"; break; } }); connect(btn5,&QPushButton::clicked,[&](){ // if(QMessageBox::Yes == QMessageBox::question( // this, // tr("Question"), // tr("Are you OK?"), // QMessageBox::Yes|QMessageBox::No, // QMessageBox::Yes)) // { // QMessageBox::information(this,tr("Hmmm..."), // tr("I'm glad to hear that!")); // } // else{ // QMessageBox::information(this, // tr("Hmmm..."), // tr("I'm sorry!")); // } QMessageBox msgBox; }); }