包括文件标准对话框、颜色对话框、字体对话框、输入对话框、消息对话框、自定义对话框
2、ui 3、代码 3.1 头文件private slots:
void ShowFile();
void ShowColor();
void ShowFont();
void ShowInput();
void ShowQuestionMsg();
void ShowCustomDlg();
3.2 源文件
#include "QtWidgetsApplication.h" #include4、结果展示#pragma execution_character_set("utf-8") #include #include #include #include QtWidgetsApplication::QtWidgetsApplication(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); setWindowTitle("对话框汇总"); connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(ShowFile())); connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(ShowColor())); connect(ui.pushButton_3, SIGNAL(clicked()), this, SLOT(ShowFont())); connect(ui.pushButton_4, SIGNAL(clicked()), this, SLOT(ShowInput())); connect(ui.pushButton_5, SIGNAL(clicked()), this, SLOT(ShowQuestionMsg())); connect(ui.pushButton_6, SIGNAL(clicked()), this, SLOT(ShowCustomDlg())); } // 文件对话框 void QtWidgetsApplication::ShowFile() { QString FilePath = QFileDialog::getOpenFileName(this, "选择文本文件", "/", "文本文件(*.txt)"); ui.label->setText(FilePath); // QString FilePath = QFileDialog::getExistingDirectory(this, "选择文件夹"); } // 颜色对话框 void QtWidgetsApplication::ShowColor() { QColor qc = QColorDialog::getColor(Qt::red); ui.label_2->setAutoFillBackground(true); if (qc.isValid()) { ui.label_2->setStyleSheet(tr("background-color:%1").arg(qc.name())); } } // 字体对话框 void QtWidgetsApplication::ShowFont() { bool ok; QFont qf = QFontDialog::getFont(&ok); if (ok) { ui.label_3->setFont(qf); } } // 输入对话框 void QtWidgetsApplication::ShowInput() { // 字符串输入框 bool ok1; // 父窗口,标题,提示,输入模式,默认文字,窗体标识 QString text = QInputDialog::getText(this, tr("字符串对话框"), tr("请输入字符串"), QLineEdit::Normal, "nrr", &ok1); if (ok1 && !text.isEmpty()) { ui.label_4->setText(text); } // 标准条目对话框 QStringList SexItems; SexItems << tr("男") << tr("女"); bool ok2; // 父窗口,标题,提示,可选条目,默认条目,文字是否可编辑,窗体标识 QString SexItem = QInputDialog::getItem(this, tr("标准条目对话框"), tr("请选择性别"), SexItems, 0, false, &ok2); // int输入框 bool ok3; //窗口,标题,提示, 默认值,最小,最大,步长,窗口标识 int age = QInputDialog::getInt(this, tr("int类型对话框"), tr("请输入年龄:"), 13, -3, 100, 4, &ok3); // double bool ok4; //窗口,标题,提示, 默认值,最小,最大,步长,窗口标识 double score = QInputDialog::getDouble(this, tr("double类型对话框"), tr("请输入成绩:"), 13, -3, 100, 0.5, &ok4); } // 消息对话框 void QtWidgetsApplication::ShowQuestionMsg() { // qusetion 消息框 ui.label_5->setText(tr("nrr")); // 主窗口、标题、提示信息、选项、焦点默认 switch (QMessageBox::question(this, tr("Question消息框"), tr("您已完成修改,是否要结束程序?"), QMessageBox::Ok | QMessageBox::No, QMessageBox::Ok)) { case QMessageBox::Ok: ui.label_5->setText(tr("QMessageBox::Ok")); break; case QMessageBox::No: ui.label_5->setText(tr("QMessageBox::No")); break; default: break; } // Warning消息框 // 主窗口、标题、提示信息、选项、焦点默认 switch (QMessageBox::warning(this, tr("Warning消息框"), tr("您修改的还没保存,是否要结束程序?"), QMessageBox::Save | QMessageBox::Discard, QMessageBox::Save)) { case QMessageBox::Ok: ui.label_5->setText(tr("QMessageBox::Save")); break; case QMessageBox::No: ui.label_5->setText(tr("QMessageBox::Discard")); break; default: break; } // Critical消息框 // 主窗口、标题、提示信息、选项、焦点默认 QMessageBox::critical(this, tr("Critical消息框"), tr("这是Critical消息框测试"), QMessageBox::Ok | QMessageBox::No, QMessageBox::Ok); // About消息框 // 主窗口、标题、提示信息 QMessageBox::about(this, tr("About消息框"), tr("这是About消息框测试")); // About Qt消息框 // 主窗口、标题 QMessageBox::aboutQt(this, tr("About消息框")); } // 自定义对话框 void QtWidgetsApplication::ShowCustomDlg() { QMessageBox qm_ni; qm_ni.setWindowTitle(tr("自定义")); // 自定义按钮 文本、描述 QPushButton* yesBtn = qm_ni.addButton(tr("耶斯"), QMessageBox::ActionRole); QPushButton* noBtn = qm_ni.addButton(tr("no"), QMessageBox::ActionRole); //QPushButton* cancelBtn = qm_ni.addButton(QMessageBox::Cancel); // 加入标准按钮 让addbutton按顺序添加 qm_ni.setText(tr("自定义消息框")); // 文字提示 // qm_ni.setIconPixmap(QPixmap("ni.jpg")); // 图标 qm_ni.exec(); // 显示 if (qm_ni.clickedButton() == yesBtn) { ui.label_6->setText(tr("yes")); } else if (qm_ni.clickedButton() == noBtn) { ui.label_6->setText(tr("no")); } }



