今天我们来讲Qt提供的标准对话框。这些对话框不仅用法简单,而且在实际项目中都很实用。这些东西只需要熟悉一下,有个初步印象即可,等到用的时候再行查看不迟。
今天的内容可以查看Qt的例子,standard widget。
新建GUI项目StandardDialogs,类名StandardDialogs,基类选择QWidget。绘制如下的对话框:
2.5.1 输入对话框
void StandardDialogs::on_btn_getint_clicked() { bool ok; int i = QInputDialog::getInt(this, tr("情况"), tr("几岁毕业"), 25, 16, 100, 1, &ok); if (ok) ui->label_input->setText(tr("%1").arg(i)); } void StandardDialogs::on_btn_getdouble_clicked() { bool ok; double d = QInputDialog::getDouble(this, tr("工作"), tr("一天挣多少钱"), 1000, 100, 10000, 1, &ok); if (ok) ui->label_input->setText(QString("%1").arg(d)); } void StandardDialogs::on_btn_getitem_clicked() { QStringList items; items << tr("王者荣耀") << tr("Dota") << tr("Dota2") << tr("LOL"); bool ok; QString item = QInputDialog::getItem(this, tr("游戏"), tr("你玩哪一个"), items, 0, false, &ok); if (ok && !item.isEmpty()) ui->label_input->setText(item); } void StandardDialogs::on_btn_gettext_clicked() { bool ok; QString text = QInputDialog::getText(this, tr("发展"), tr("想跳槽吗"), QLineEdit::Normal, tr("懒"), &ok); if (ok && !text.isEmpty()) ui->label_input->setText(text); } void StandardDialogs::on_btn_getmtext_clicked() { bool ok; QString text = QInputDialog::getMultiLineText(this, tr("计划"), tr("下一步怎么做"), "走着瞧", &ok); if (ok && !text.isEmpty()) ui->label_input->setText(text); }
2.5.2 颜色对话框
void StandardDialogs::on_btn_getcolor_clicked() { QColorDialog::ColorDialogOptions option; if (ui->check_alpha->isChecked()) { option = QColorDialog::ShowAlphaChannel; } const QColor color = QColorDialog::getColor(Qt::green, this, "选择颜色", option); if (color.isValid()) { ui->btn_getcolor->setText(color.name()); ui->btn_getcolor->setPalette(QPalette(color)); ui->btn_getcolor->setAutoFillBackground(true); } }
2.5.3 字体对话框
void StandardDialogs::on_btn_font_clicked() { bool ok; QFont font = QFontDialog::getFont(&ok, ui->label_font->font(), this, tr("选择字体")); if (ok) { ui->label_font->setFont(font); } }
2.5.4 文件对话框
void StandardDialogs::on_btn_dir_clicked() { QString directory = QFileDialog::getExistingDirectory(this, tr("获取文件夹"), "./"); if (!directory.isEmpty()) ui->line_path->setText(directory); } void StandardDialogs::on_btn_file_clicked() { QString fileName = QFileDialog::getOpenFileName(this, tr("打开一个文件"), "./", tr("All Files (*);;Text Files (*.txt)")); if (!fileName.isEmpty()) ui->line_path->setText(fileName); } void StandardDialogs::on_btn_files_clicked() { QStringList files = QFileDialog::getOpenFileNames( this, tr("打开多个文件"), "./", tr("All Files (*);;Text Files (*.txt)")); if (files.count()) ui->line_path->setText(QString("[%1]").arg(files.join(", "))); } void StandardDialogs::on_btn_savefile_clicked() { QString fileName = QFileDialog::getSaveFileName(this, tr("保存文件"), "./", tr("All Files (*);;Text Files (*.txt)")); if (!fileName.isEmpty()) ui->line_path->setText(fileName); }
2.5.5 提示对话框
void StandardDialogs::on_btn_critical_clicked() { QMessageBox::critical(this, tr("危险"), tr("修路危险"), tr("返回"), tr("步行"), tr("无视")); } void StandardDialogs::on_btn_info_clicked() { QMessageBox::information(this, tr("通知"), tr("会有个女朋友吗"), tr("不可能"), tr("真不可能")); } void StandardDialogs::on_btn_question_clicked() { QMessageBox::question(this, tr("问题"), "喜欢身材好的还是颜值高的", QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); } void StandardDialogs::on_btn_warning_clicked() { QMessageBox msgBox(QMessageBox::Warning, tr("警告"), "不要调戏女朋友", 0, this); msgBox.setDetailedText(tr("真的别")); msgBox.addButton(tr("Save &Again"), QMessageBox::AcceptRole); msgBox.addButton(tr("&Continue"), QMessageBox::RejectRole); if (msgBox.exec() == QMessageBox::AcceptRole) qDebug() << "accept"; } void StandardDialogs::on_btn_message_clicked() { message.showMessage(tr("恭喜你中了5个亿,不好意思,系统出错")); }
2.5.6 进度对话框
void StandardDialogs::on_btn_progress_clicked() { int numFiles = 50000; QProgressDialog progress("复制文件中...", "中断", 0, numFiles, this); progress.setWindowModality(Qt::WindowModal); progress.show(); for (int i = 0; i < numFiles; i++) { progress.setValue(i); QCoreApplication::processEvents(); if (progress.wasCanceled()) break; } progress.setValue(numFiles); }
2.5.7 安装向导对话框
void StandardDialogs::on_btn_wizard_clicked() { QWizard w(this); w.setWindowTitle("向导"); QWizardPage* page1 = new QWizardPage(); QWizardPage* page2 = new QWizardPage(); QWizardPage* page3 = new QWizardPage(); page1->setTitle("第1步"); page2->setTitle("第2步"); page3->setTitle("第3步"); w.addPage(page1); w.addPage(page2); w.addPage(page3); w.exec(); }
可以通过继承QWizardPage自定义向导窗口,定制自己需要的窗口。
好了,关于标准对话框的内容就讲到这里,如果你想第一时间看到小豆君的技术分享,就赶快关注吧。
欢迎关注小豆君的微信公众号:小豆君,只要关注,便可加入小豆君为大家创建的C++\Qt交流群,方便讨论学习。