【嵌入式——QT】标准对话框
- 文件对话框
- 颜色对话框
- 字体对话框
- 输入对话框
- 消息框
- 代码示例
文件对话框
QFileDialog
常用静态函数
- getOpenFileName:选择打开一个文件;
- getOpenFileNames:选择打开多个文件;
- getSaveFileName:选择保存一个文件;
- getExistingDirectory:选择一个已有的目录;
- getOpenFileUrl:选择打开一个文件,可选择远程网络文件;
图示
颜色对话框
QColorDialog
常用静态函数
- getColor:选择颜色;
图示
字体对话框
QFontDialog
常用静态函数
- getFont:选择字体;
图示
输入对话框
QInputDialog
常用静态函数
- getText:输入单行文字;
- getInt:输入整数;
- getDouble:输入浮点数;
- getItem:从一个下拉列表框中选择输入;
- getMultiLineText:输入多行字符串;
图示
消息框
QMessageBox
常用静态函数
- information:信息提示对话框;
- question:询问并获取是否确认的对话框;
- warning:警告信息提示对话框;
- critical:错误信息提示对话框;
- about:设置自定义信息的关于对话框;
- aboutQt:关于Qt的对话框;
图示
代码示例
DialogDialog.h
#ifndef DIALOGDIALOG_H
#define DIALOGDIALOG_H#include <QDialog>namespace Ui
{class DialogDialog;
}class DialogDialog : public QDialog
{Q_OBJECTpublic:explicit DialogDialog(QWidget* parent = nullptr);~DialogDialog();private slots:void on_pushButtonOpen_clicked();void on_pushButtonOpens_clicked();void on_pushButtonSelect_clicked();void on_pushButtonSave_clicked();void on_pushButtonColor_clicked();void on_pushButtonFont_clicked();void on_pushButtonQuestion_clicked();void on_pushButtonInformation_clicked();void on_pushButtonWarning_clicked();void on_pushButtonCritical_clicked();void on_pushButtonAbout_clicked();void on_pushButtonAboutQt_clicked();void on_pushButtonStr_clicked();void on_pushButtonInt_clicked();void on_pushButtonFloat_clicked();void on_pushButtonItem_clicked();void on_pushButtonClear_clicked();private:Ui::DialogDialog* ui;
};#endif // DIALOGDIALOG_H
DialogDialog.cpp
#include "DialogDialog.h"
#include "ui_DialogDialog.h"
#include <QDir>
#include <QFileDialog>
#include <QColorDialog>
#include <QFontDialog>
#include <QLineEdit>
#include <QInputDialog>
#include <QMessageBox>
DialogDialog::DialogDialog(QWidget* parent): QDialog(parent), ui(new Ui::DialogDialog)
{ui->setupUi(this);
}DialogDialog::~DialogDialog()
{delete ui;
}void DialogDialog::on_pushButtonOpen_clicked()
{QString curPath = QDir::currentPath();QString aFileName = QFileDialog::getOpenFileName(this, u8"选择一个文件", curPath);if(!aFileName.isEmpty()) {ui->plainTextEdit->appendPlainText(aFileName);}
}void DialogDialog::on_pushButtonOpens_clicked()
{QString curPath = QDir::currentPath();QStringList aFileNames = QFileDialog::getOpenFileNames(this, u8"选择一个文件", curPath);for(int i=0; i<aFileNames.count(); i++) {ui->plainTextEdit->appendPlainText(aFileNames.at(i));}
}void DialogDialog::on_pushButtonSelect_clicked()
{QString curPath = QCoreApplication::applicationDirPath();QString selectedDir = QFileDialog::getExistingDirectory(this, u8"选择一个目录", curPath, QFileDialog::ShowDirsOnly);if(!selectedDir.isEmpty()) {ui->plainTextEdit->appendPlainText(selectedDir);}
}void DialogDialog::on_pushButtonSave_clicked()
{QString curPath = QCoreApplication::applicationDirPath();QString fileName = QFileDialog::getSaveFileName(this, u8"保存文件", curPath);if(!fileName.isEmpty()) {ui->plainTextEdit->appendPlainText(fileName);}
}void DialogDialog::on_pushButtonColor_clicked()
{QPalette pal = ui->plainTextEdit->palette();//获取现有paletteQColor initColor = pal.color(QPalette::Text);//现有文字颜色QColor color = QColorDialog::getColor(initColor, this, u8"选择颜色");if(color.isValid()) {pal.setColor(QPalette::Text, color);ui->plainTextEdit->setPalette(pal);}
}void DialogDialog::on_pushButtonFont_clicked()
{QFont initFont = ui->plainTextEdit->font();bool ok = false;QFont font = QFontDialog::getFont(&ok, initFont);if(ok) {ui->plainTextEdit->setFont(font);}
}void DialogDialog::on_pushButtonQuestion_clicked()
{QMessageBox::StandardButton defaultBtn = QMessageBox::NoButton;QMessageBox::StandardButton result;result = QMessageBox::question(this, u8"question消息框", u8"文件已被修改,是否保存", QMessageBox::Yes|QMessageBox::No | QMessageBox::Cancel, defaultBtn);if(result == QMessageBox::Yes) {ui->plainTextEdit->appendPlainText(u8"quesstion消息框 Yes 被选择");} else if(result == QMessageBox::No) {ui->plainTextEdit->appendPlainText(u8"quesstion消息框 No 被选择");} else if(result == QMessageBox::Cancel) {ui->plainTextEdit->appendPlainText(u8"quesstion消息框 Cancel 被选择");} else {ui->plainTextEdit->appendPlainText(u8"quesstion消息框无选择");}
}void DialogDialog::on_pushButtonInformation_clicked()
{QMessageBox::information(this, u8"information消息框", u8"文件已经打开,字体大小已设置", QMessageBox::Ok, QMessageBox::NoButton);
}void DialogDialog::on_pushButtonWarning_clicked()
{QMessageBox::warning(this, u8"warning消息框", u8"文件内容已被修改");
}void DialogDialog::on_pushButtonCritical_clicked()
{QMessageBox::critical(this, u8"critical消息框", u8"有不明程序访问网络");
}void DialogDialog::on_pushButtonAbout_clicked()
{QMessageBox::about(this, u8"about消息框", u8"软件版本1.0.0");
}void DialogDialog::on_pushButtonAboutQt_clicked()
{QMessageBox::aboutQt(this, u8"aboutQt消息框");
}void DialogDialog::on_pushButtonStr_clicked()
{QLineEdit::EchoMode echoMode = QLineEdit::Normal;bool ok = false;QString text = QInputDialog::getText(this, u8"输入文字对话框", u8"输入文件名", echoMode, u8"新建文件.txt", &ok);if(ok && !text.isEmpty()) {ui->plainTextEdit->appendPlainText(text);}
}void DialogDialog::on_pushButtonInt_clicked()
{int defaultValue = ui->plainTextEdit->font().pointSize();int minValue = 6, maxValue = 50, stepValue = 1;bool ok = false;int inputValue = QInputDialog::getInt(this, u8"输入整数对话框", u8"设置字体大小", defaultValue, minValue, maxValue, stepValue, &ok);if(ok) {QFont font = ui->plainTextEdit->font();font.setPointSize(inputValue);ui->plainTextEdit->setFont(font);}
}void DialogDialog::on_pushButtonFloat_clicked()
{float defaultValue = 3.13;float minValue = 0, maxValue = 1000;int decimals = 2;bool ok = false;float inputValue = QInputDialog::getDouble(this, u8"输入浮点数对话框", u8"输入浮点数", defaultValue, minValue, maxValue, decimals, &ok);if(ok) {QString str = QString::asprintf(u8"输入一个浮点数:%.2f", inputValue);ui->plainTextEdit->appendPlainText(str);}
}void DialogDialog::on_pushButtonItem_clicked()
{QStringList items;items<<u8"优秀"<<u8"良好"<<u8"合格"<<u8"不合格";int curIndex = 0;bool editable = true;bool ok = false;QString text = QInputDialog::getItem(this, u8"条目选择对话框", u8"选择级别", items, curIndex, editable, &ok);if(ok && !text.isEmpty()) {ui->plainTextEdit->appendPlainText(text);}
}void DialogDialog::on_pushButtonClear_clicked()
{ui->plainTextEdit->clear();
}